2017年11月30日 星期四

[C_AR01-易] 一維陣列反轉 I(C++)

[C_AR01-易] 一維陣列反轉 I

題目描述:
一維陣列反轉
輸入說明:
輸入一個一維陣列,元素最多不超過100個
輸出說明:
輸出反轉後的陣列
最後需有換行。
範例:

輸入範例:輸出範例:
4 6 3 69 234
56 89 23 3 1
176 5 890 643 2
0 500 6 634 55 123
87 77 32 22 111 4
234 69 3 6 4
1 3 23 89 56
2 643 890 5 176
123 55 634 6 500 0
4 111 22 32 77 87


  1. #include <iostream>  
  2. #include<sstream>  
  3. #include<algorithm>  
  4.   
  5. using namespace std;  
  6.   
  7. int main() {  
  8.     // [C_AR01-易] 一維陣列反轉 I  
  9.     string s;  
  10.     string str[100];//切割後放入str[]  
  11.     int count = 0; //計算有幾個數字  
  12.     while(getline(cin , s))//切割  
  13.     {  
  14.         stringstream ss(s);//將s字串塞入ss  
  15.         string token; //將切割後的結果存入token  
  16.         while (getline(ss, token, ' '))  
  17.         {  
  18.             str[count] = token;  
  19.             count++;  
  20.         }  
  21.         reverse(str, str+count); // 反轉陣列  
  22.         for(int i = 0;i < count;i++)  
  23.         {  
  24.             if(i != count-1)  
  25.             {  
  26.                 cout << str[i] << " ";  
  27.             }  
  28.             else  
  29.             {  
  30.                 cout << str[i] << endl;  
  31.             }  
  32.         }  
  33.         count = 0;  
  34.     }  
  35.     return 0;  
  36. }  

沒有留言:

張貼留言