2017年12月16日 星期六

[ITSA Exam.60] Problem 2. 解讀神秘的密碼(C++)

Problem 2. 解讀神秘的密碼
(Time Limit: 2 seconds)

問題描述 :
你突然發現螢幕上出現幾行詭異的字串,隨後收到一封電子郵件。信上說這幾行字其實是反轉句,將它還原就可以得到原本想傳達的訊息。請寫出一程式來還原這些反轉句。

輸入說明:

第一行輸入一正整數 N(1≤N≤10),表示共有 N 筆測資,每筆測資為一英文字串,字串長度最多不超過 50 個字元。

輸出說明:

此字串的反轉句。

範例:



Sample Input:
Sample Output:
3
retupmoc ecneics dlroW olleH
computer science Hello World


  1. #include <iostream>  
  2. #include<string>  
  3. #include<algorithm>  
  4.    
  5. using namespace std;  
  6.    
  7. int main() {  
  8.     // Problem 2. 解讀神秘的密碼  
  9.     int n;  
  10.     cin >> n;  
  11.     cin.ignore();   //預防下面的getline讀到空白
  12.     for(int i = 0;i < n;i++)  
  13.     {  
  14.         string text;  
  15.         getline(cin, text);  
  16.         reverse(text.begin(), text.end());  //字串反轉
  17.         cout << text << endl;  
  18.     }  
  19.     return 0;  
  20. }  

沒有留言:

張貼留言