[PAT-A 1036]Boys vs Girls

在这里插入图片描述
题目大意:
给出N个同学的信息,输出女生之中的最高分数获得者和男生之中最低分数获得者的姓名学号与两个乘积之差

思路:
1.定义结构体person保存学生信息,包括姓名,学号,性别,分数
2.定义F_lowest与M_highest保存男生中最高分获得者与女生之中最低分获得者,初始化男生最低分101,女生最高分-1
3.用temp保存输入,并判断
4.输出,注意没有女生/男生的情况

AC代码:

	//PAT_A 1036
	#include<cstdio>
	using namespace std;
	struct person {
		char name[15];
		char id[15];
		char gender;
		int gread;
	}M_lowest, F_highest, temp;
	void init() {
		M_lowest.gread = 101;
		F_highest.gread = -1;
	}
	int main() {
		init();
		int n;
		(void)scanf("%d", &n);
		for (int i = 0; i < n; i++) {
			(void)scanf("%s %c %s %d", temp.name, &temp.gender, temp.id, &temp.gread);
			if (temp.gender == 'F' && temp.gread > F_highest.gread)F_highest = temp;
			else if (temp.gender == 'M' && temp.gread < M_lowest.gread)M_lowest = temp;
		}
		if (F_highest.gread == -1)printf("Absent\n");
		else printf("%s %s\n", F_highest.name, F_highest.id);
		if (M_lowest.gread == 101)printf("Absent\n");
		else printf("%s %s\n", M_lowest.name, M_lowest.id);
		if (F_highest.gread == -1 || M_lowest.gread == 101)printf("NA");
		else printf("%d", F_highest.gread - M_lowest.gread);
		return 0;
	}
发布了101 篇原创文章 · 获赞 1 · 访问量 3049

猜你喜欢

转载自blog.csdn.net/weixin_44699689/article/details/103948795