题解 | #句子逆序#

句子逆序

https://www.nowcoder.com/practice/48b3cb4e3c694d9da5526e6255bb73c3

#include <iostream>
#include <stack>    
using namespace std;

int main()
{
    string input, new_str;
    stack<string> s;    //使用string(单词)组成的stack实现逆序输出的效果
    while (cin >> input)    //while循环中cin遇到空格和回车均不会结束
    {
        s.push(input);
        if (cin.get() == '\n')  //使用get()判断是否遇到空格
            break;
    }
    while (!s.empty())
    {
        cout << s.top();
        s.pop();
        if (!s.empty())
            cout << " ";
    }
    return 0;
}

全部评论

相关推荐

06-27 15:29
门头沟学院 Java
点赞 评论 收藏
分享
程序员饺子:正常 我沟通了200多个 15个要简历 面试2个 全投的成都的小厂。很多看我是27直接不会了😅
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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