PAT A1036 Boys vs Girls (25)

PTA跳转:原题链接

这道题目首先输入1个整型n,接着输入n行数据每行数据包括“姓名,性别,学号,成绩”四项信息。找出女生里面成绩最高的和男生里面成绩最低的,前两行分别输出这位女生和男生的姓名和学号,第三行输出这两位学生的成绩之差。

如果输入数据里面没有男生或者没有女生,再进行特殊处理。

代码:

#include <iostream>
using namespace std;

struct{
    string name;
    char gender;
    string id;
    int score;
}Student[5000];	//存储学生信息

int main(){
    int n;  //学生个数
    cin >> n;
    for(int i=0; i<n; i++){
        cin >> Student[i].name >> Student[i].gender >> Student[i].id >> Student[i].score;
    }
    int lloc = 5000, hloc = -1, lowest = 100, highest = -1;
    for(int i=0; i<n; i++){
    	if(Student[i].gender == 'M' && Student[i].score < lowest){
    		lowest = Student[i].score;
    		lloc = i;
		}
		else if(Student[i].gender == 'F' && Student[i].score > highest){
    		highest = Student[i].score;
    		hloc = i;
		}
	}
	if(hloc != -1){
		cout << Student[hloc].name << " " << Student[hloc].id << endl;
	}
	else{
		cout << "Absent" << endl;
	}
	if(lloc != 5000){
		cout << Student[lloc].name << " " << Student[lloc].id << endl;
	}
	else{
		cout << "Absent" << endl;
	}
	if(hloc == -1 || lloc == 5000){
		cout << "NA";
	} 
	else{
		cout << highest-lowest;
	} 
    return 0;
}
发布了19 篇原创文章 · 获赞 21 · 访问量 4528

猜你喜欢

转载自blog.csdn.net/koori_145/article/details/104396786