PAT A1036

1036 Boys vs Girls (25 分)

This time you are asked to tell the difference between the lowest grade of all the male students and the highest grade of all the female students.

Input Specification:

Each input file contains one test case. Each case contains a positive integer N, followed by N lines of student information. Each line contains a student's namegenderID and grade, separated by a space, where name and ID are strings of no more than 10 characters with no space, gender is either F (female) or M (male), and grade is an integer between 0 and 100. It is guaranteed that all the grades are distinct.

Output Specification:

For each test case, output in 3 lines. The first line gives the name and ID of the female student with the highest grade, and the second line gives that of the male student with the lowest grade. The third line gives the difference grade​F​​−grade​M​​. If one such kind of student is missing, output Absent in the corresponding line, and output NA in the third line instead.

Sample Input 1:

3
Joe M Math990112 89
Mike M CS991301 100
Mary F EE990830 95

Sample Output 1:

Mary EE990830
Joe Math990112
6

Sample Input 2:

1
Jean M AA980920 60

Sample Output 2:

Absent
Jean AA980920
NA

男孩 vs 女孩

现在你被告知 说出 所有男孩的最低成绩 和 所有女孩的最后成绩 的差异

输入格式:

每个输入文件有一个测试用例,每个用例包含

一个正整数 N,然后 N 行学生信息。

每行包含一个学生的 姓名、性别、ID、成绩,

姓名 和 ID 都是不超过10个字符没有空格的 字符串, 性别 不是 F 就是 M,成绩是 1-100 之间的整数。每个成绩都是独特的。

输出格式:

对于每个用例,输出3行。

第一行给出 男孩 最高成绩 的 姓名、ID;

第二行给出 女孩 最低成绩 的 姓名、ID;

第三行给出 两个成绩的不同。

如果一种类型的学生不存在,则在相应行输出 Absent,然后在第三行 输出 NA

思路:

创建一个结构体存储学生信息,用 count 计数器排查有没有此类学生输入,输入信息,如果是女孩,则和女孩的最高成绩比较,如果比它高,则替换。如果 是男孩,则和男孩的最低成绩比较,如果比它更低,则替换。最后根据 count 计数器的值来输出指定输出方案。

#include <stdio.h>
#include <string.h>
struct{
	char name[15];
	char gender[5];
	char id[15];
	int score;
}stu[10010];

int main(){
	int n;
	scanf("%d", &n);
	char c1[2] = {'F'};
	int min = 101;
	int max = -1;
	int kmin, kmax;
	int count1 = 0;
	int count2 = 0;
	for(int i = 0; i < n; i++){
		scanf("%s %s %s %d", stu[i].name, stu[i].gender, stu[i].id, &stu[i].score);
		int cmp = strcmp(c1, stu[i].gender);
		if(cmp == 0){
			// 女孩
			if(stu[i].score > max){
				max = stu[i].score;
				kmax = i;
				count1++;
			}
		}
		else{
			// 男孩
			if(stu[i].score < min){
				min = stu[i].score;
				kmin = i;
				count2++;
			}

		}		
	}
	if(count1 != 0 && count2 != 0){
		printf("%s %s\n", stu[kmax].name, stu[kmax].id);
		printf("%s %s\n", stu[kmin].name, stu[kmin].id);
		printf("%d", max-min);
	}else if(count1 == 0){
		printf("Absent\n");
		printf("%s %s\n", stu[kmin].name, stu[kmin].id);
		printf("NA");
	}
	else if(count2 == 0){
		printf("%s %s\n", stu[kmax].name, stu[kmax].id);
		printf("Absent\n");
		printf("NA");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/gsj9086/article/details/85387219