1004. Ranking (20)
time limit
400ms
memory limit
65536 kB
code length limit
8000B
Judgment procedure
Standard
author
CHEN, Yue
Read the names, student numbers, and grades of n students, and output the names of the students with the highest grades and the lowest grades respectively and student number.
Input format: Each test input contains 1 test case in the format of
Line 1: positive integer n Line 2: the first student's name, student number, grade Line 3: The name of the second student Student ID Grade ... ... ... Row n+1: the nth student's name, student ID, grade
The name and student number are strings of no more than 10 characters, and the grade is an integer between 0 and 100. Here, it is guaranteed that no two students have the same grade in a set of test cases.
Output format: Output 2 lines for each test case, the first line is The name and student ID of the student with the highest grade, the second line is the name and student ID of the student with the lowest grade, with 1 space between the strings.
Sample input:
3 Joe Math990112 89 Mike CS991301 100 Mary EE990830 95
Sample output:
Mike CS991301 Joe Math990112
#include #include using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char** argv) { string n1; getline(cin,n1); int n; if(n1. length()==1) n=n1[0]-'0'; else if(n1. length()==2) n=(n1[0]-'0')*10+n1[1]-'0'; else n=(n1[0]-'0')*100+(n1[1]-'0')*10+(n1[2]- '0'); string s[1000]; for(int i=0;i<n;i++) getline(cin,s[i]); int a[1000]; for(int i=0;i<n;i++) { int j=0; int k=0; while(k!=2) { if(s[i][j]==' ') k++; j++; } j--; if(s[i].length()-j-1==3) a[i]=100; else if(s[i].length()-j-1==2) a[i]=(s[i][j+1]-'0')*10+(s[i][j+2]- '0'); else a[i]=(s[i][j+1]-'0'); } int max=0,min=0; for(int i=1;i<n;i++) { if(a[i]>a[max]) max=i; if(a[i]<a[min]) min=i; } int i=0; int j=0; while(i!=2) { cout<<s[max][j]; j++; if(s[max][j]==' ') i++; } i=0; j=0; cout<<endl; while(i!=2) { cout<<s[min][j]; j++; if(s[min][j]==' ') i++; } return 0; }