pat 甲级 A1036 Boys vs Girls (25分)

题目链接:

https://pintia.cn/problem-sets/994805342720868352/problems/994805453203030016

 

题目大意:

通过给出的男生和女生的分数,输出分数最高女生和分数最低男生的姓名和ID,以及两者分数之差。若某一性别的学生不存在,则输出Absent,相应分数之差用NA代替。

 

思路分析:

设置一个长度为2的数组score[2]存储男生和女生的最低分和最高分。初始值设为,101,-1。在输入的时候找出到此次为止女生分数最高的信息,以及男生分数最低的信息。

 

参考代码:

#include <cstdio>

struct Pnode{
    char name[11];
    char gd;
    char id[11];
    int grade;
}stu[2], temp;

int main(){
    int n;
    scanf("%d",&n);
    int score[2];
    score[0] = -1; //求女孩最高分,设初始值为-1;
    score[1] = 101; //男孩最低分初始值设为101.
    for(int i = 0; i < n; i++){
        scanf("%s %c %s %d",temp.name, &temp.gd, temp.id, &temp.grade);
        if( temp.gd == 'F'){
            if( score[0] < temp.grade){
                stu[0] = temp; 
                score[0] = temp.grade;
            }
        }else{
            if( score[1] > temp.grade){
                stu[1] = temp; 
                score[1] = temp.grade;
            }
        }
    }
    if(score[0] != -1) printf("%s %s\n",stu[0].name, stu[0].id);
    else  printf("Absent\n");
    if(score[1] != 101) printf("%s %s\n",stu[1].name, stu[1].id);
    else printf("Absent\n");
    if(score[0] != -1  && score[1] != 101) printf("%d",stu[0].grade - stu[1].grade);
    else printf("NA");
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值