题解 | #句子逆序#
句子逆序
https://www.nowcoder.com/practice/48b3cb4e3c694d9da5526e6255bb73c3
#include <algorithm> #include <cstddef> #include <iostream> using namespace std; int main() { string str; getline(cin, str); // 获取字符串 reverse(str.begin(), str.end()); // 先反转一下字符串 size_t len = str.length(); // 逐个翻转单词 for(size_t i = 0; i < len; i++){ size_t j = i; while(str[j] != ' ' && j < len){ ++j; } reverse(str.begin()+i, str.begin()+j); i = j; } cout << str << endl; } // 64 位输出请用 printf("%lld")