小错误:
-
string 和 char 的区别:
string sex = "F"
char sex = 'F'
-
这道题同甲1006,需要不断更新结果。在处理第一个人的输入,你要先把它赋给答案,再去更新,这里用闫老师的写法
if (boy_name.empty() || score < min_score)
:如果是第一个人,或者分数小于最值就更新。
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
string girl_name, girl_id;
string boy_name, boy_id;
int min_score, max_score;
int n;
cin >> n;
for(int i = 0; i < n; i ++ )
{
string name, sex, id;
int score;
cin >> name >> sex >> id >> score;
if (sex == "M")
{
if (boy_name.empty() || score < min_score)
{
min_score = score;
boy_name = name;
boy_id = id;
}
}
if (sex == "F")
{
if (girl_name.empty() || score > max_score)
{
max_score = score;
girl_name = name;
girl_id = id;
}
}
}
if (girl_name.empty()) cout << "Absent" << endl;
else cout << girl_name << ' ' << girl_id << endl;
if (boy_name.empty()) cout << "Absent" << endl;
else cout << boy_name << ' ' << boy_id << endl;
if (girl_name.empty() || boy_name.empty()) cout << "NA"<< endl;
else cout << abs(max_score - min_score) << endl;
return 0;
}