2018年1月30日 星期二

[C_SO39-易] 團體排名(C++)

[C_SO39-易] 團體排名

問題描述 :
國際競賽中,總共有 150 項的比賽項目,主辦單位根據各個國家所得到的獎牌數目及種類(金、銀、銅牌)進行團體排名。排名規則為:先比較金牌數,若金牌數相同,則比較銀牌數;若金、銀牌數均相同,則比較銅牌數;若三者均相同,則排名相同。請根據各個國家所得到的獎牌數目及種類,找出冠軍國家。
輸入說明 :
程式輸入的第一行包含一個正整數 n , 1 ≤ n ≤ 100 ,代表接下來有 n 行資料,每一行包含一個字串 ( 國家名稱 ) 和三個整數 ( 金、銀、銅牌的個數 ) ,資料間以一個空格隔開。
輸出說明 :
輸出冠軍的國家。如果有不只一個國家冠軍,則每一行輸出一個國家的名稱,輸出的順序以國家名稱的字典排序。
範例 :

輸入範例輸出範例
3
US 20 13 34
CN 19 23 43
JP 10 15 20
US



  1. #include <iostream>  
  2. #include<string>  
  3. #include<tuple>  
  4.    
  5. using namespace std;  
  6.    
  7. int main() {  
  8.     // [C_SO39-易] 團體排名  
  9.     int n;  
  10.     cin >> n;  
  11.     tuple<stringintintint> country[n];  
  12.     string name;  
  13.     int g, s, c;  
  14.     for(int i = 0;i < n;i++)  
  15.     {  
  16.         cin >> name >> g >> s >> c;  
  17.         country[i] = make_tuple(name, g, s, c);  
  18.     }  
  19.     //排金牌數量  
  20.     for (int i = 0; i < n; i++)  
  21.     {  
  22.         int pos = i;  
  23.         for (int j = i; j < n; j++)  
  24.         {  
  25.             if (get<1>(country[j]) > get<1>(country[pos]))  
  26.             {  
  27.                 pos = j;  
  28.             }   
  29.         }  
  30.         swap(country[pos], country[i]);  
  31.     }   
  32.     //排銀牌數量  
  33.     for (int i = 0; i < n; i++)  
  34.     {  
  35.         int pos = i;  
  36.         for (int j = i; j < n; j++)  
  37.         {  
  38.             if (get<1>(country[j]) == get<1>(country[pos]) && get<2>(country[j]) > get<2>(country[pos]))  
  39.             {  
  40.                 pos = j;  
  41.             }   
  42.         }  
  43.         swap(country[pos], country[i]);  
  44.     }  
  45.     //排銅牌數量  
  46.     for (int i = 0; i < n; i++)  
  47.     {  
  48.         int pos = i;  
  49.         for (int j = i; j < n; j++)  
  50.         {  
  51.             if (get<1>(country[j]) == get<1>(country[pos]) && get<2>(country[j]) == get<2>(country[pos]) && get<3>(country[j]) > get<3>(country[pos]))  
  52.             {  
  53.                 pos = j;  
  54.             }   
  55.         }  
  56.         swap(country[pos], country[i]);  
  57.     }   
  58.     //找出第一名  
  59.     string ch[100];  
  60.     c = 0;  
  61.     for(int i = 0;i < n;i++)  
  62.     {  
  63.         if(get<1>(country[i])==get<1>(country[0])&&get<2>(country[i])==get<2>(country[0])&&get<3>(country[i])==get<3>(country[0]))  
  64.         {  
  65.             ch[c] = get<0>(country[i]);  
  66.             c++;  
  67.         }  
  68.     }  
  69.     //輸出  
  70.     if(c == 1)  
  71.     {  
  72.         cout << get<0>(country[0]) << endl;  
  73.     }  
  74.     else  
  75.     {  
  76.         for (int i = 0; i < c; i++)  
  77.         {  
  78.             int pos = i;  
  79.             for (int j = i; j < c; j++)  
  80.             {  
  81.                 if (int(ch[j][0]) < int(ch[pos][0]))  
  82.                 {  
  83.                     pos = j;  
  84.                 }   
  85.             }  
  86.             swap(ch[pos], ch[i]);  
  87.         }   
  88.         for(int i = 0;i < c;i++)  
  89.         {  
  90.             cout << ch[i] << endl;  
  91.         }  
  92.     }  
  93.     return 0;  
  94. }  

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. }