题解 | 多组_带空格的字符串_T组形式
多组_带空格的字符串_T组形式
https://www.nowcoder.com/practice/cff28a28d7f54419a640a8bb19f4275f
#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, ""); }