PTA c++部分预备知识

一、函数题

(1) 6-1 函数调用 (运用指针直接指向对应的函数,进行调用)

·样例程序

在这里给出函数被调用进行测试的例子。例如:
#include <iostream>
using namespace std;

int max(int a, int b);
int min(int a, int b);
int sum(int a, int b);
int compute(int a, int b, int(*func)(int, int));

int main()
{
int a, b, res;
 cin >> a >> b;
res = compute(a, b, & max);
cout << "Max of " << a << " and " << b << " is " << res << endl;
res = compute(a, b, & min);
cout << "Min of " << a << " and " << b << " is " << res << endl;
res = compute(a, b, & sum);
cout << "Sum of " << a << " and " << b << " is " << res << endl;
return 0;
}

/* 请在这里填写答案 */

int compute(int a, int b, int(*func)(int, int)) 个人感觉这个地方比较重要

int max(int a, int b){
	if(a>b)return a;
	return b;
}
int min(int a, int b){
	if(a<b)return a;
	return b;
}
int sum(int a, int b){
	return a+b;
}
int compute(int a, int b, int(*func)(int, int)){
	
	return func(a,b); //主要知识点int(*func)(int, int)运用指针,直接定向对应函数
}

(2)6-2 函数指针(理科实验班)

·裁判样例

#include <iostream>
using namespace std;
const int N=80;
struct Student{
  int num;
  int score[4];
};

/* 请在这里填写答案 */

int main()
{
  int i, j, k, n;
  bool s2(const Student &, const Student &);
  bool s4(const Student &, const Student &);
  Student st[N];
   cin>>n;
   for(i=0;i<n;i++){
      cin>>st[i].num;
      for(j=0;j<4;j++) cin>>st[i].score[j];
    }
   cout<<select(st, n, s2)<<endl;
   cout<<select(st, n, s4)<<endl;
}

 参考sort函数的第三个函数

和之前的一道题好像!用sort+结构体真的好香!

ps:一开始还想过用命名空间直接把select改成sort(迷惑想法,不太可行)

bool s2(const Student &a,const Student &b){
	if(a.score[0]+a.score[1]!=b.score[0]+b.score[1])
	return a.score[0]+a.score[1]>b.score[0]+b.score[1];
	else 
	return a.num<b.num;
}
bool s4(const Student &a,const Student &b){ //好多地方可以优化,但...懒虫冲向下一题了
	if(a.score[0]+a.score[1]+a.score[2]+a.score[3]!=b.score[0]+b.score[1]+b.score[2]+b.score[3])
	return a.score[0]+a.score[1]+a.score[2]+a.score[3]>b.score[0]+b.score[1]+b.score[2]+b.score[3];
	else 
	return a.num<b.num;
}
int select(Student st[],int n,bool(*s)(const Student &a,const Student &b)){
	int maxn=0;
	for(int i=1;i<n;i++)
		if(!s(st[maxn],st[i]))maxn=i;
	return st[maxn].num;
}

(3)6-3 逆序字符串

·裁判样例

#include <iostream>
#include <string>
using namespace std;

/* 你的代码将嵌在这里 */

int main(int argc, char const *argv[])
{
    string str;
    getline(cin, str);        //输入字符串
    reverse_string(str);     //逆序字符串str
    cout << str << endl;    //输出逆序后的字符串
    return 0;
}

 注意:千万别忘了取地址&,不然只能局部修改,没法真正影响到字符串

void reverse_string(string &s){ //睁大小眼睛,差点忘了取地址&
	string s2=s;
	int n=s.size()-1;
	for(int i=0;i<s.size();i++)s[i]=s2[n--];
	return ;
}

(4)6-4 带默认形参值的函数

·裁判样例

#include <iostream>
using namespace std;

/* 请在这里填写答案 */


int main()
{
    int a, b, c;

    cin >> a >> b >> c;

    cout << add(a) << endl;
    cout << add(a, b) << endl;
    cout << add(a, b, c) << endl;

    return 0;
}

 带默认值的函数,在传参的时候预设一下就好了

int add(int x,int y=20,int z=30){
	return x+y+z;
}

(5)6-5 引用作函数形参交换两个整数

·裁判样例

#include <iostream>
using namespace std;

/* 请在这里填写答案 */

int main()
{
    int a, b;

    cin >> a >> b;

    Swap(a, b);

    cout << a << " " << b << endl;

    return 0;
}

老熟人了,就不说了

void Swap(int &a,int &b){
	int tmp=a;
	a=b;
	b=tmp;
} 

 二、错题分析区

1.形参 int fun(int a=1,int b,int c=2)是否合法?
不合法,指定了默认值的形参后,其右边不能出现没有默认值的形参,即默认值集中在形参列表的右边。同时,不能指定引用参数的默认值(int & m = 4不可以),引用参数不能用常量赋值

  • 12
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值