题解 | #句子逆序#

句子逆序

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();
    }
}
全部评论

相关推荐

JamesGosling1:同一个公司的实习为什么写三次,就算是不同的小组的话,直接写一段要好点吧
点赞 评论 收藏
分享
点赞 评论 收藏
分享
阿里巴巴各部门年终奖开奖了,有人拿到了220w
真烦好烦真烦:拿命换钱呢,公司给你220万,肯定是因为你对公司的贡献大于220万,想想要多厉害多累才能达到
投递阿里巴巴集团等公司10个岗位 >
点赞 评论 收藏
分享
评论
3
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务