题解 | #句子逆序#

句子逆序

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-23 11:43
门头沟学院 Java
allin校招的烤冷...:我靠,今天中午我也是这个hr隔一个星期发消息给我。问的问题还是一模一样的😅
点赞 评论 收藏
分享
05-09 14:45
门头沟学院 Java
点赞 评论 收藏
分享
评论
3
收藏
分享

创作者周榜

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