Run ID:158725
提交时间:2026-08-22 16:52:10
#include <iostream> #include <string> #include <algorithm> using namespace std; int main() { // 优化输入输出效率 ios::sync_with_stdio(false); cin.tie(0); int T; if (!(cin >> T)) return 0; cin.ignore(); // 关键:吸收掉读取T之后留下的换行符,防止影响第一次getline string line; while (T--) { getline(cin, line); // 读取一整行 string word = ""; for (int i = 0; i < line.length(); i++) { if (line[i] != ' ') { word += line[i]; // 收集字母 } else { // 遇到空格,说明当前单词结束 reverse(word.begin(), word.end()); cout << word; cout << ' '; // 输出空格,保持原有的空格数量 word = ""; // 清空临时单词 } } // 循环结束后,处理最后一个单词(如果有的话) if (!word.empty()) { reverse(word.begin(), word.end()); cout << word; } // 每个测试样例输出后换行 cout << '\n'; } return 0; }