1036 Boys vs Girls (25 分)

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 name, gender, ID 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 gradeF?gradeM. 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

思路

代码

#include<stdio.h>
#include<string.h>

struct Student{
	char name[20];
	char ID[20];
	char gender;
	int grade;
}tmp, highest, lowest;

bool Higher(Student a, Student b){//判断同性别的学生的成绩A成绩高时返回true 
	if(a.gender == b.gender) return a.grade > b.grade;
	else return false;
}

void Init(){//初始化highest 和lowest 的相关数据 
	lowest.gender = ‘M‘;
	lowest.grade = 100;
	highest.gender = ‘F‘;
	highest.grade = 0;
}

int main(){
	int n;
	int girl = 0;//boy和girl分别来记录输入的男女生的人数,若没有女生或者没有男生输入,后面还要特判
	int boy = 0;
	scanf("%d", &n);
	Init();
	for(int i = 0; i < n; i++){
		scanf("%s %c %s %d", &tmp.name, &tmp.gender, &tmp.ID, &tmp.grade);
		if(tmp.gender == ‘M‘ && Higher(lowest, tmp)) {
			lowest = tmp;
			boy++;
		}
		if(tmp.gender == ‘F‘ && Higher(tmp, highest)){
			highest = tmp;
			girl++;
		} 
	}
	if(boy != 0 && girl != 0){//男孩女孩成绩都有
		printf("%s %s\n", highest.name, highest.ID);
		printf("%s %s\n%d", lowest.name, lowest.ID, highest.grade - lowest.grade);
	}else if(boy == 0){//没有男孩子,特判
		printf("%s %s\n", highest.name, highest.ID);
		printf("Absent\nNA");
	}else{//没有女孩子,特判
		printf("Absent\n");
		printf("%s %s\nNA", lowest.name, lowest.ID);
	}
	return 0;
}

参考代码

  1. 思路与上面的差不多,但是其中的初始化男生的女生的分数时,设置了101和-1分,这样我们可以通过判断最后是否存在101和-1来看有没有男女生的输入来完成特判,就不需要我上面用到的两个辅助变量girl和boy了
  2. 成绩的比较没有那么麻烦(就一句话),可以不用另外写函数
#include<stdio.h>

struct Student{
	char name[15];
	char ID[15];
	char gender;
	int grade;
}tmp, highest, lowest;

void Init(){//初始化highest 和lowest 的相关数据 
	lowest.grade = 101;
	highest.grade = -1;
}

int main(){
	int n;
	scanf("%d", &n);
	Init();
	for(int i = 0; i < n; i++){
		scanf("%s %c %s %d", &tmp.name, &tmp.gender, &tmp.ID, &tmp.grade);
		if(tmp.gender == ‘M‘ && tmp.grade < lowest.grade) {
			lowest = tmp;
		}
		if(tmp.gender == ‘F‘ && tmp.grade > highest.grade){
			highest = tmp;
		} 
	}
	
	if(highest.grade == -1) printf("Absent\n");
	else printf("%s %s\n", highest.name, highest.ID);
	if(lowest.grade == 101) printf("Absent\n");
	else printf("%s %s\n", lowest.name, lowest.ID);
	if(highest.grade == -1 || lowest.grade == 101) printf("NA\n");
	return 0;
}

1036 Boys vs Girls (25 分)

上一篇:【JIRA】jira issue reindex


下一篇:HDU6956 Pass! 常系数齐次线性递推 BSGS