2018年1月29日 星期一

[C_SO06-中] 考試成績排名(C++)

[C_SO06-中] 考試成績排名

問題描述 :
某次段考後,老師為了分發獎狀,希望用這次考的數學和英文兩科來決定學生的名次,其中以數學分數為主,當數學分數較高時,其名次亦較高;倘若數學同分,則再以英文的分數來決定名次高低。

輸入說明 :
第一列的整數 n ( 0 < n < 100 ),代表班上學生總人數,其後有 n 列,每一列代表學生的學號和數學、英文成績,前者是數學成績,後者是英文成績。各項資料分別用空白分開。

輸出說明 :
印出總成績的排名。各項資料分別用空白分開。

範例 :

輸入範例輸出範例
10
1 90 75
2 75 60
3 69 69
4 86 66
5 73 80
6 73 95
7 90 69
8 67 78
9 39 73
10 77 59
1 90 75
7 90 69
4 86 66
10 77 59
2 75 60
6 73 95
5 73 80
3 69 69
8 67 78
9 39 73
10
1 80 75
2 90 67
3 50 69
4 86 66
5 73 80
6 44 95
7 69 82
8 68 11
9 41 73
10 52 96
2 90 67
4 86 66
1 80 75
5 73 80
7 69 82
8 68 11
10 52 96
3 50 69
6 44 95
9 41 73



  1. #include <iostream>  
  2. #include<string>  
  3. #include<tuple>  
  4.   
  5. using namespace std;  
  6.   
  7. int main() {  
  8.     // [C_SO06-中] 考試成績排名  
  9.     int n;  
  10.     cin >> n;  
  11.     tuple<intintint> Student[n];  
  12.     int stu_id, m, e;  
  13.     for(int i = 0;i < n;i++)  
  14.     {  
  15.         cin >> stu_id >> m >> e;  
  16.         Student[i] = make_tuple(stu_id, m, e);  
  17.     }  
  18.     //排數學分數  
  19.     for (int i = 0; i < n; i++)  
  20.     {  
  21.         int pos = i;  
  22.         for (int j = i; j < n; j++)  
  23.         {  
  24.             if (get<1>(Student[j]) > get<1>(Student[pos]))  
  25.             {  
  26.                 pos = j;  
  27.             }   
  28.         }  
  29.         swap(Student[pos], Student[i]);  
  30.     }   
  31.     //排英文分數  
  32.     for (int i = 0; i < n; i++)  
  33.     {  
  34.         int pos = i;  
  35.         for (int j = i; j < n; j++)  
  36.         {  
  37.             if (get<1>(Student[j]) == get<1>(Student[pos]) && get<2>(Student[j]) > get<2>(Student[pos]))  
  38.             {  
  39.                 pos = j;  
  40.             }   
  41.         }  
  42.         swap(Student[pos], Student[i]);  
  43.     }   
  44.     //輸出  
  45.     for(int i = 0;i < n;i++)  
  46.     {  
  47.         cout << get<0>(Student[i]) << " " << get<1>(Student[i])  << " " << get<2>(Student[i]) << endl;  
  48.     }  
  49.     return 0;  
  50. }  

沒有留言:

張貼留言