zoukankan      html  css  js  c++  java
  • 函数指针(理科实验班)

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

    输入将首先输入学生数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



    --------------------------------------------------------------------------------------
                    参考代码
    --------------------------------------------------------------------------------------


    #include <iostream>
    using namespace std;
    const int N=80;
    struct Student{
      int num;
      int score[4];
    };
    
    /* 请在这里填写答案 */
    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)
    {
        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 true;
        else 
            return false;
    }     
    int select (Student st[],int n,bool (*s)(const Student &,const Student &))//主函数使用const,这里必须有const 
    {
        int maxNum=0;
        for(int i=1;i<n;i++)
        {
            if(s(st[maxNum],st[i]))
                continue;
            else 
                maxNum=i;
        }
        return st[maxNum].num;
    }
    
    
    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;
    }

    欢迎指教,一起学习!

    未经本人允许,请勿转载!

    谢谢!

     
    个人分享,欢迎指导,未经允许,请勿转载。谢谢!
  • 相关阅读:
    noi.ac #30 思维
    bzoj 2330: [SCOI2011]糖果
    bzoj 2326: [HNOI2011]数学作业
    bzoj 2324: [ZJOI2011]营救皮卡丘
    bzoj 2301: [HAOI2011]Problem b
    bzoj 2286: [Sdoi2011消耗战
    bzoj 2282: [Sdoi2011]消防
    bzoj 2257: [Jsoi2009]瓶子和燃料
    bzoj 2245: [SDOI2011]工作安排
    bzoj 2244: [SDOI2011]拦截导弹
  • 原文地址:https://www.cnblogs.com/hello-OK/p/6930499.html
Copyright © 2011-2022 走看看