OOP_Week2

6-1 函数指针(理科实验班) (10分)
 

梦山高中现需要将某普通班的最优秀学生调整入理科实验班。为此,将从两个方面考察学生,一是数学和英语两门课的总分;另一个是所有四门课的总分。分别找出两科总分和全科总分的第一名,并从中决定调整人选。

输入将首先输入学生数n, (n为不超过80的正整数);接下来依次输入各位学生的学号,数学、英语、语文、理科综合成绩。学号及四科成绩均为不超过300的正整数。

输出时:第一行输出两科总分第一的学号,第二行输出四科总分第一的学号。 约定在两位学生成绩相同时,优先选择学号较小的学生;各位学生的学号均不相同。

裁判测试程序样例:

#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;
}

输入样例:

3
6 148 150 120 252
5 148 150 117 260
7 145 148 128 287
 

输出样例:

5
7

 
   
bool s2(const Student &a,const Student &b)
{
    if(a.score[0]+a.score[1]>b.score[0]+b.score[1])return true;
    else return false;
}
bool s4(const Student &a,const Student &b)
{
    int aa,bb;
    aa=a.score[0]+a.score[1]+a.score[2]+a.score[3];
    bb=b.score[0]+b.score[1]+b.score[2]+b.score[3];
    if(aa>bb)return true;
    else return false;
}
int select(Student st[],int n,bool (*p)(const Student &, const Student &))
{
    int i,max=0;
    for(i=0;i<n;i++)
    {
        if(p(st[max],st[i]))continue;
        else max=i;
    }
    return st[max].num;
}
 
   

 注意指针函数的用法

(*p)(st[max],st[i])
 
 
p(st[max],st[i])
都可以




















猜你喜欢

转载自www.cnblogs.com/KIDKK/p/12422515.html