#include <iostream> #include <string> #include <algorithm> using namespace std; int main() { int t, n; size_t bstart {}, bend {}; string s; cin >> t; while(t--) { cin >> n; cin.ignore(); // 忽略前一个输入后的换行符 getline(cin, s); while (true) { bstart = s.find(' '); if (bstart == string::npos) break; bend = s.find(' ', bstart); s.replace(bstart, bend-bstart+1, ""); } reverse(s.begin(), s.end()); cout << s << endl; } }
去除字符串空格的代码如下:
while (true) { bstart = s.find(' '); if (bstart == string::npos) break; bend = s.find(' ', bstart); s.replace(bstart, bend-bstart+1, ""); }