基本思路
- 构造Person结构体,属性,姓名,性别,编号,分数
- 初始化Girl与Boy属性,在遍历中记录Girl最高分和Boy最低分
- 检查是否缺失男性或者女性,按照题目要求输出对应格式
#include <bits/stdc++.h> using namespace std; struct Person{ int score; string name, id, sex; }Girl, Boy, temp; void init(){ Girl.score = 0; Girl.sex = 'F'; Boy.score = 100; Boy.sex = 'M'; } int main(int argc, char* argv[]) { init(); int n, score; cin >> n; string name, id, sex; bool flag_M, flag_F; flag_M = flag_F = false; for(int i = 0; i < n; i++){ cin >> name >> sex >> id >> score; if(sex == "M"){ flag_M = true; if(score < Boy.score){ Boy.id = id; Boy.name = name; Boy.score = score; } }else if(sex == "F"){ flag_F = true; if(score > Girl.score){ Girl.id = id; Girl.name = name; Girl.score = score; } } } bool flag = false; // 不存在女生 if(flag_F == false){ flag = true; cout << "Absent" << endl; }else{ cout << Girl.name << ' ' << Girl.id << endl; } // 不存在男生 if(flag_M == false){ flag = true; cout << "Absent" << endl; }else{ cout << Boy.name << ' ' << Boy.id << endl; } if(flag){ cout << "NA" << endl; }else{ cout << (Boy.score < Girl.score ? Girl.score - Boy.score : Boy.score - Girl.score) << endl; } return 0; }
睁开眼,书在面前 闭上眼,书在心里