题解 | #句子逆序#

句子逆序

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")

全部评论

相关推荐

WhiteAlbum...:学院本2中大厂垂直实习➕acm比赛 秋招0面试
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务