PAT甲级 1036. Boys vs Girls (25)
2017-11-07 19:36
302 查看
题目:
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:
Sample Output 1:
Sample Input 2:
Sample Output 2:
思路:
这个可用vector的排序,先排性别再排分数,这样排序之后,一般序列第一个是女最高分,最后一个是男最低分。
需注意只有一个学生和没有学生的情况。这次的代码因为逻辑简单,没有在排序之后,再进行进一步处理,就直接分情况输出了,代码写得有点繁琐。
代码:
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
struct student
{
string name;
char gender;
string ID;
int grade;
bool operator<(const student &s1)const
{
if (this->gender != s1.gender)
{
return this->gender < s1.gender;
}
else
{
return this->grade > s1.grade;
}
}
};
int main()
{
//input
int N;
cin >> N;
int i = 0;
vector<student> stu(N);
for (; i < N; ++i)
{
cin >> stu[i].name >> stu[i].gender >> stu[i].ID >> stu[i].grade;
}
sort(stu.begin(),stu.end());
if (N >=2)
{
if (stu[0].gender == 'F')
{
cout << stu[0].name << " " << stu[0].ID << endl;
i = stu.size() - 1;
if (stu[i].gender == 'M')
{
cout<< stu[i].name << " " << stu[i].ID << endl;
cout << stu[0].grade - stu[i].grade << endl;
}
else
{
cout << "Absent" << endl;
cout << "NA"<< endl;
}
}
else
{
cout << "Absent" << endl;
if (stu[i].gender == 'M')
{
cout << stu[i].name << " " << stu[i].ID << endl;
}
cout << "NA" << endl;
}
}
else
{
if (N == 0)
{
cout<< "Absent" << endl;
cout << "Absent" << endl;
cout << "NA" << endl;
}
if (N == 1)
{
if (stu[0].gender == 'F')
{
cout << stu[0].name << " " << stu[0].ID << endl;
cout<< "Absent" << endl;
}
else
{
cout << "Absent" << endl;
cout << stu[0].name << " " << stu[0].ID << endl;
}
cout << "NA" << endl;
}
}
system("pause");
return 0;
}
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
思路:
这个可用vector的排序,先排性别再排分数,这样排序之后,一般序列第一个是女最高分,最后一个是男最低分。
需注意只有一个学生和没有学生的情况。这次的代码因为逻辑简单,没有在排序之后,再进行进一步处理,就直接分情况输出了,代码写得有点繁琐。
代码:
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
struct student
{
string name;
char gender;
string ID;
int grade;
bool operator<(const student &s1)const
{
if (this->gender != s1.gender)
{
return this->gender < s1.gender;
}
else
{
return this->grade > s1.grade;
}
}
};
int main()
{
//input
int N;
cin >> N;
int i = 0;
vector<student> stu(N);
for (; i < N; ++i)
{
cin >> stu[i].name >> stu[i].gender >> stu[i].ID >> stu[i].grade;
}
sort(stu.begin(),stu.end());
if (N >=2)
{
if (stu[0].gender == 'F')
{
cout << stu[0].name << " " << stu[0].ID << endl;
i = stu.size() - 1;
if (stu[i].gender == 'M')
{
cout<< stu[i].name << " " << stu[i].ID << endl;
cout << stu[0].grade - stu[i].grade << endl;
}
else
{
cout << "Absent" << endl;
cout << "NA"<< endl;
}
}
else
{
cout << "Absent" << endl;
if (stu[i].gender == 'M')
{
cout << stu[i].name << " " << stu[i].ID << endl;
}
cout << "NA" << endl;
}
}
else
{
if (N == 0)
{
cout<< "Absent" << endl;
cout << "Absent" << endl;
cout << "NA" << endl;
}
if (N == 1)
{
if (stu[0].gender == 'F')
{
cout << stu[0].name << " " << stu[0].ID << endl;
cout<< "Absent" << endl;
}
else
{
cout << "Absent" << endl;
cout << stu[0].name << " " << stu[0].ID << endl;
}
cout << "NA" << endl;
}
}
system("pause");
return 0;
}
相关文章推荐
- PAT甲级真题及训练集(10)--1036. Boys vs Girls (25)
- 【PAT甲级】1036. Boys vs Girls (25)
- PAT 甲级 1036. Boys vs Girls (25)
- 1036. Boys vs Girls (25)-PAT甲级真题
- PAT甲级1036. Boys vs Girls (25)
- 浙江大学PAT_甲级_1036. Boys vs Girls (25)
- PAT(甲级)1036. Boys vs Girls (25)
- 【PAT甲级】1036. Boys vs Girls (25)——JAVA实现
- PAT甲级.1036. Boys vs Girls (25)
- PAT (Advanced Level) 1036. Boys vs Girls (25) 结构体排序
- PAT 1036. Boys vs Girls (25)(简单成绩排序)
- PAT-A-1036. Boys vs Girls (25)
- 【C++】PAT(advanced level)1036. Boys vs Girls (25)
- PAT刷题:1036. Boys vs Girls (25)
- PAT (Advanced Level) 1036. Boys vs Girls (25) 解题报告
- PAT甲题题解-1036. Boys vs Girls (25)-找最大最小,大水题
- PAT (Advanced Level) Practise 1036 Boys vs Girls (25)
- PAT 1036. Boys vs Girls (25)
- 【PAT】【Advanced Level】1036. Boys vs Girls (25)
- PAT 1036. Boys vs Girls (25)