Run ID 作者 问题 语言 测评结果 Time Memory 代码长度 提交时间
152638 Kevin 字符串反转 C++ Accepted 2 MS 300 KB 1111 2026-04-29 19:25:49

Tests(1/1):


Code:

#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; // 安全跳过n后面的换行 cin.ignore(numeric_limits<streamsize>::max(), '\n'); for (int i = 1; i <= n; ++i) { string s; getline(cin, s); int len = s.size(); int cnt = 0; for (int j = 0; j < len; ++j) { if (s[j] == ' ') { // 输出当前单词的反转 for (int c = j - 1; c >= cnt; --c) { cout << s[c]; } // 输出空格 cout << ' '; // 更新cnt为下一个单词的起始位置(空格的下一个字符) cnt = j + 1; } } // 输出最后一个单词的反转 for (int c = len - 1; c >= cnt; --c) { cout << s[c]; } // 换行(如果题目不允许最后一行有换行,可以用i!=n时才输出) cout << '\n'; } return 0; }