题解 | #句子逆序#

句子逆序

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

学习和记录一下

方法1:

#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
    string s;
    vector<string> v;
    while (cin >> s) {
        v.push_back(s);
    }
    for (auto it = v.rbegin(); it != v.rend(); it++) {
        cout << *it << " ";
    }
}

方法2

#include <iostream>
#include <vector>
#include <string>
#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    vector<string> v;
    while (cin >> s) {
        v.emplace_back(s);
    }
    copy(v.rbegin(), v.rend(), ostream_iterator<string> (cout, " "));
}

方法3

#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main()
{
    string str;
    stack<string> sta;
    while (cin >> str) {
        sta.push(str);
    }
    while (!sta.empty()) {
        cout << sta.top() << " ";
        sta.pop();
    }
}
全部评论

相关推荐

06-26 15:33
青岛工学院 Java
积极的秋田犬要冲国企:他现在邀请我明天面试
点赞 评论 收藏
分享
fRank1e:吓得我不敢去外包了,但是目前也只有外包这一个实习,我还要继续去吗
点赞 评论 收藏
分享
评论
3
收藏
分享

创作者周榜

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