標籤:cout list card ret 學生 info swa 測試案例 class
1004 成績排名 (20 分)
讀入 n(>0)名學生的姓名、學號、成績,分別輸出成績最高和成績最低學生的姓名和學號。
輸入格式:
每個測試輸入包含 1 個測試案例,格式為
第 1 行:正整數 n第 2 行:第 1 個學生的姓名 學號 成績第 3 行:第 2 個學生的姓名 學號 成績 ... ... ...第 n+1 行:第 n 個學生的姓名 學號 成績
輸入範例:
3Joe Math990112 89Mike CS991301 100Mary EE990830 95
輸出範例:
Mike CS991301Joe Math990112
1 #include "stdafx.h" 2 #include <algorithm> 3 #include <iostream> 4 5 using namespace std; 6 struct student { 7 char name[10]; 8 char num[10]; 9 int score;10 }stu[10010];11 bool com(student a, student b)12 {13 return a.score < b.score;14 15 }16 void swap(student a, student b) {17 student c;18 c = a;19 a = b;20 b = c; 21 }22 int main()23 {24 int n;25 cin >> n;26 for (int i = 0; i < n; i++) {27 cin >> stu[i].name >> stu[i].num >> stu[i].score;28 }29 for (int i = 0; i <= n; i++) {30 if (com(stu[i], stu[i + 1]))31 swap(stu[i], stu[i + 1]);32 }33 for (int i = n; i>=0; i--) {34 if(i==n-1)35 cout << stu[i].name <<" "<< stu[i].num<<"\n";36 if (i == 0)37 cout << stu[i].name<<" " << stu[i].num ;38 39 }40 41 42 return 0;43 }
/******* 為什麼會列印出y*****************/
1004 成績排名 未解決的問題