[PAT Basic] 1004. standings

1004 standings (20 points)

Source title

Reads  the n-( >) student's name, school, achievement, output the highest score and the lowest score the student's name and student number.

Input formats:

Each test comprises a test input format

第 1 行:正整数 n
第 2 行:第 1 个学生的姓名 学号 成绩
第 3 行:第 2 个学生的姓名 学号 成绩
  ... ... ...
第 n+1 行:第 n 个学生的姓名 学号 成绩

Wherein 姓名and 学号are no more than 10 characters in the string, the result is an integer between 0 and 100, where a set of test cases to ensure that no two are the same student achievement.

Output formats:

For each test case output 2, line 1 is the highest score a student's name and student number, the second line is the lowest score the student's name and student number, there is a space between the strings.

Sample input:

3
Joe Math990112 89
Mike CS991301 100
Mary EE990830 95

Sample output:

Mike CS991301
Joe Math990112

analysis:

Student defined structure to save the student name, student number, scores, compare scores to find the minimum and maximum of the line

 

 1 #include <iostream>
 2 using namespace std;
 3 
 4 typedef struct Student
 5 {
 6     string name;
 7     string number;
 8     unsigned int score;
 9 }Student;
10 
11 int main()
12 {
13     int n;
14     cin >> n;
15     Student *stu = new Student[n];
16     Student max, min;
17 
18     for (int i = 0; i < n; ++i)
19     {
20         cin >> stu[i].name >> stu[i].number >> stu[i].score;
21     }
22     max = stu[0];
23     min = stu[0];
24     for (int i = 1; i < n; ++i)
25     {
26         if (stu[i].score > max.score)
27         {
28             max = stu[i];
29             continue;
30         }
31         if (stu[i].score < min.score)
32         {
33             min = stu[i];
34         }
35         
36     }
37     cout << max.name << " " << max.number << endl;
38     cout << min.name << " " << min.number;
39     delete[] stu;
40     return 0;
41 }

 

Guess you like

Origin www.cnblogs.com/47Pineapple/p/11361401.html
Recommended