題目地址:1323. Switch text
思路:
題目意思不好理解呀。
題目意思是這樣的:輸入兩個測試數據,首先,兩個測試數據本身得各自前後倒轉,然後兩個測試數據倒轉後的結果再各自對半互換,然後測試數據二先輸出,測試數據一再輸出,不斷循環下去。還有一點很關鍵,就是對空行的判斷,這個空行可以是空格組成,或者是直接回車,空行是忽略不輸出的。
還是直接看代碼吧,這樣好理解一點。用stl會使程序簡化明了很多,具體如下:
1 #include <iostream>
2 #include <string>
3 #include <algorithm>
4 using namespace std;
5
6 bool isempty(string x) {
7 for (int i = 0; i < x.size(); i++)
8 if (x[i] != ' ')
9 return false;
10 return true;
11 }
12
13 void print(string x) {
14 if (isempty(x)) return;
15 string temp;
16 temp.append(x.begin()+x.size()/2, x.end());
17 temp.append(x.begin(), x.begin()+x.size()/2);
18 cout << temp << endl;
19 }
20
21 int main() {
22 string str1, str2;
23 while (getline(cin, str1) && getline(cin, str2)) {
24 reverse(str1.begin(), str1.end());
25 reverse(str2.begin(), str2.end());
26 print(str2);
27 print(str1);
28 }
29
30 return 0;
31 }