1036. Boys vs Girls (25)

简介:

题目链接:http://www.patest.cn/contests/pat-a-practise/1036

题目:

1036. Boys vs Girls (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

分析:

找到分数最高的女生和分数最低的男生的差值。

注意没有人(男生或女生)的情况。

AC代码:

#include<iostream>
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
struct Student{
 string name;
 char sex;
 string id;
 int grade;
};
vector<Student> M;//男生
vector<Student> F;//女生
Student M_lowest;//分数最低的男生
Student F_highest;//分数最高的女生
int main(){
 //freopen("F://Temp/input.txt", "r", stdin);
 int n;
 cin >> n;
 for (int i = 0; i < n; i++){
  Student tmp;
  cin >> tmp.name >> tmp.sex >> tmp.id >> tmp.grade;
  if (tmp.sex == 'M')M.push_back(tmp);
  else F.push_back(tmp);
 }
 M_lowest.grade = 100;
 for (int i = 0; i < M.size(); i++){
  if (M_lowest.grade > M[i].grade){
   M_lowest = M[i];
  }
 }
 F_highest.grade = 0;
 for (int i = 0; i < F.size(); i++){
  if (F_highest.grade < F[i].grade){
   F_highest = F[i];
  }
 }
 if (M_lowest.grade == 100 && F_highest.grade == 0){//说明没有找到
  cout << "Absent" << endl << "Absent" << endl << "NA" << endl;
 }
 else if (M_lowest.grade == 100){
  cout << F_highest.name << " " << F_highest.id << endl << "Absent" << endl << "NA" << endl;
 }
 else if (F_highest.grade == 0){
  cout << "Absent" << endl << M_lowest.name << " " << M_lowest.id << endl << "NA" << endl;
 }
 else {//都找到了
  cout << F_highest.name << " " << F_highest.id << endl << M_lowest.name << " " << M_lowest.id << endl << F_highest.grade - M_lowest.grade << endl;
 }
 return 0;
}


截图:










本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5114689.html,如需转载请自行联系原作者

相关文章
|
安全
LeetCode 213. House Robber II
你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金。这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的。同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。 给定一个代表每个房屋存放金额的非负整数数组,计算你在不触动警报装置的情况下,能够偷窃到的最高金额。
59 0
LeetCode 213. House Robber II
AtCoder Beginner Contest 225 F.String Cards(dp)
AtCoder Beginner Contest 225 F.String Cards(dp)
76 0
HDU-1027,Ignatius and the Princess II
HDU-1027,Ignatius and the Princess II
HDU-1029,Ignatius and the Princess IV
HDU-1029,Ignatius and the Princess IV
Codeforces 714A Meeting of Old Friends
A. Meeting of Old Friends time limit per test:1 second memory limit per test:256 megabytes input:standard input output:s...
833 0
|
人工智能 安全
[LeetCode]--198. House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adj
1106 0
|
Windows
English Contest -- The Programmer&#39;s Dream
<p></p> <p>Yes, The Programmer’s Dream!</p> <p>How many programmers here? I am sure of that every body here has a dream inside. What’s the biggest dream or goal you have in your life at this mom
1685 0
|
安全
House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping yo...
865 0