2017年10月29日 星期日

[C_AR53-易] 學生成績排序(C++)

[C_AR53-易] 學生成績排序

問題描述:
請撰寫一個程式,使用者會輸入學生人數,以及每位學生的成績 (0-99) ,程式會將成績的排序輸出。
輸入說明:
使用者輸入學生人數 n ,以及每位學生的成績 (0~99) 。
輸出說明:
根據使用者輸入的成績,顯示排序的結果 ( 由低到高 ) 。
範例:
Sample Input:Sample Output:
10
50
89
25
2
25
12
52
65
10
79
2
10
12
25
25
50
52
65
79
89
  1. #include <iostream>  
  2. #include <algorithm>//sort()  
  3. using namespace std;  
  4.   
  5. int main() {  
  6.     // [C_AR53-易] 學生成績排序  
  7.     int n;//學生數  
  8.     cin >> n;  
  9.     int grade[n];//成績  
  10.     for(int i = 0;i < n;i++)  
  11.     {  
  12.         cin >> grade[i];  
  13.     }  
  14.     sort(grade,grade+n);  
  15.     for(int i = 0;i < n;i++)  
  16.     {  
  17.         cout << grade[i] << endl;;  
  18.     }  
  19.     return 0;  
  20. }  

[C_ST17-易] 判斷是否為迴文(C++)

[C_ST17-易] 判斷是否為迴文

問題描述:迴文是指從前面讀和從後面讀都相同的一個數字或一段文字。例如下列每一五位數的整數都是迴文: 123321 , 55555 , 45554 , 11611 。請撰寫一個程式,判斷它是否迴文。
輸入說明輸入一個正整數。
輸出說明:迴文印出 ” 是 ” ;非回文印出 ” 否 ” 。範例:

Sample Input:Sample Output:
123321
1556551
1244221
YES
YES
NO

  1. #include <iostream>  
  2. #include <cmath>  
  3. #include <string.h>  
  4.    
  5. using namespace std;  
  6.    
  7. int main() {  
  8.     // [C_ST17-易] 判斷是否為迴文  
  9.     int count; //string 長度  
  10.     string input;  
  11.     bool re = true//判斷是否回文,起始都是回文  
  12.     while(cin >> input)  
  13.     {  
  14.         count = input.size();//string 長度  
  15.         char result[count]; //存入回文結果  
  16.         strcpy(result, input.c_str());//string to char  
  17.         for(int i = 0;i<floor(count/2);i++)//製造回文  
  18.         {  
  19.             if(result[i] != result[count-1-i])  
  20.             {  
  21.                 re = false;//有一個不等於,就不是回文  
  22.             }  
  23.         }  
  24.         if(re == true)//輸出  
  25.         {  
  26.             cout << "YES" << endl;  
  27.         }  
  28.         else  
  29.         {  
  30.             cout << "NO" << endl;  
  31.         }  
  32.           
  33.     }  
  34.     return 0;  
  35. }  

[C_ST16-易] 將五位數數字分別印出(C++)

[C_ST16-易] 將五位數數字分別印出

問題描述:撰寫一個程式,輸入一個五位數的數字,將這個數字分成個別的數字,然後分別印出每個數字,數字中間必須相隔 3 個空格。若輸入 42139 ,則程式必須印出: 4 2 1 3 9 。
輸入說明輸入一個正整數。
輸出說明:輸出樣式如範例。範例:

Sample Input:Sample Output:
42139
12345
4   2   1   3   9
1   2   3   4   5

  1. #include <iostream>  
  2. #include <string.h>  
  3. using namespace std;  
  4.   
  5. int main() {  
  6.     // [C_ST16-易] 將五位數數字分別印出  
  7.     string num;  
  8.     while(cin >> num)  
  9.     {  
  10.         int count = num.size();  
  11.         char re[count];  
  12.         strcpy(re, num.c_str());//string to char  
  13.         for(int i = 0;i < count;i++)//輸出  
  14.         {  
  15.             if(i == 4)  
  16.             {  
  17.                 cout << re[i] << endl;  
  18.             }  
  19.             else  
  20.             {  
  21.                 cout << re[i] << "   ";  
  22.             }  
  23.         }  
  24.     }  
  25.     return 0;  
  26. }  

[C_ST02-易] 迴文字串(C++)

[C_ST02-易] 迴文字串

Problem Description
給予一個英文字母與數字參雜的字串,長度限制在 256 個字母內。請撰寫一程式輸出此字串的迴文字串。
Input File Format
輸入分為兩部份,第一行是介於 1 到 99 的數字,表示接著有幾個要輸入的字串。第二部份是所要改變的字串,每個字串單獨佔一列。
Output Format
經轉換後的迴文字串。
Example

Sample Input:Sample Output:
3
bcd12345
0987654321
0a1b2c3d4e5f6g 
54321dcb
1234567890
g6f5e4d3c2b1a0

  1. #include <iostream>  
  2. #include <cmath>  
  3. #include <string.h>  
  4.   
  5. using namespace std;  
  6.   
  7. int main() {  
  8.     // [C_ST02-易] 迴文字串  
  9.     int n,count;//n是測資數  
  10.     char change;//互換的暫存值  
  11.     cin >> n;  
  12.     string input;  
  13.     for(int j = 0;j < n;j++)  
  14.     {  
  15.         cin >> input;  
  16.         count = input.size();//string 長度  
  17.         char result[count]; //存入回文結果  
  18.         strcpy(result, input.c_str());//string to char  
  19.         for(int i = 0;i<floor(count/2);i++)//製造回文  
  20.         {  
  21.             change = result[i];  
  22.             result[i] = result[count-1-i];  
  23.             result[count-1-i] = change;  
  24.         }  
  25.         for(int i = 0;i<count;i++)//輸出  
  26.         {  
  27.             if(i == count-1)  
  28.             {  
  29.                 cout << result[i] << endl;  
  30.             }  
  31.             else  
  32.             {  
  33.                 cout << result[i];  
  34.             }  
  35.         }  
  36.     }  
  37.     return 0;  
  38. }  

[C_AR05-易] 最少派車數(C++)

[C_AR05-易] 最少派車數

問題描述 :
某遊覽車派遣公司共收到n筆任務訂單,訂單中詳細記載發車時間s和返回時間d。每一輛遊覽車只要任務時間不衝突,可立即更換司機繼續上路執行任務。請問該公司至少需要調遣多少車輛才足以應付需求?

輸入說明 :
程式的輸入包含兩行數字,第一行包含一個正整數n,1 ≤ n ≤ 30,代表第二行有n筆訂單的出發時間和返回時間s1, d1, s2, d2, ..., sn, dn,0 < si < di ≤ 24,而此2n個正整數間以空格隔開。
輸出說明 :
輸出最少車輛需求數。
範例 :

Sample Input:Sample Output:
3
1 6 3 12 6 18
2
  1. #include <iostream>  
  2. #include <algorithm>  
  3. using namespace std;  
  4.   
  5. int main() {  
  6.     // [C_AR05-易] 最少派車數  
  7.     int n;//測資數  
  8.     int st, et, max = 0;  
  9.     int t[25];  
  10.     fill(t,t+25,0);  
  11.     cin >> n;  
  12.     for(int j = 0;j < n;j++)  
  13.     {  
  14.         cin >> st >> et;  
  15.         for(int i = st;i < et;i++)  
  16.         {  
  17.             t[i]++;  
  18.         }  
  19.     }  
  20.     for(int k = 0;k < 25;k++)  
  21.     {  
  22.         if(t[k] > max)  
  23.         {  
  24.             max = t[k];  
  25.         }  
  26.     }  
  27.     cout << max << endl;  
  28.     return 0;  
  29. }