A1036 Boys vs Girls (25 分| 查找元素,附详细注释,逻辑分析)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_24452475/article/details/100549416

写在前面

  • 思路分析
    • string型female和male保存要求的学生信息
    • int型fscore和mscore保存男生最低分和女生最高分
      • 初始化fscore-1, mscore为最大值101
      • 根据性别、分数大小迭代更新:
        • female和male
        • fscore和mscore
  • 题目简单,不再赘述
    • 15分钟a题目

测试用例

  • input:
    3
    Joe M Math990112 89
    Mike M CS991301 100
    Mary F EE990830 95
    
    output:
    Mary EE990830
    Joe Math990112
    6
    
    input:
    1
    Jean M AA980920 60
    
    output:
    Absent
    Jean AA980920
    NA
    

ac代码

  • #include <iostream>
    using namespace std;
    
    int main()
    {
        int n;
        scanf("%d", &n);
        string female, male;
        int fscore = -1, mscore = 101;
    
        for(int i=0; i<n; i++)
        {
            string name, sex, num;
            int score;
            cin >> name >> sex >> num;
            scanf("%d", &score);
    
            if(sex == "F")
            {
                if(fscore < score)
                {
                    fscore = score;
                    female = name + " " + num;
                }
            }
            else if (mscore > score)
            {
                mscore = score;
                male = name + " " + num;
            }
        }
        if(fscore != -1)
            cout << female << endl;
        else
            printf("Absent\n");
        if(mscore != 101)
            cout << male << endl;
        else
            printf("Absent\n");
        if(mscore != 101 && fscore != -1)
            printf("%d", fscore-mscore);
        else
            printf("NA\n");
    
        return 0;
    }
    

猜你喜欢

转载自blog.csdn.net/qq_24452475/article/details/100549416