题解 | 句子逆序

句子逆序

https://www.nowcoder.com/practice/48b3cb4e3c694d9da5526e6255bb73c3?tpId=387&tqId=36837&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D1%26tpId%3D37%26type%3D387

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


/*
    可以用栈的后进先出,也可以直接用字符串倒序输出
*/
// int main() {
//     //方法一
//     stack<string>res;
//     string s;
//     while (cin >> s) {
//         //入栈
//         res.push(s);
//     }
//     while(!res.empty()){
//         //出栈
//         cout << res.top() << ' ';
//         res.pop();
//     }
// }


int main(){
    //方法二
    string str1;
    vector<string>str2;  //创建字符串数组
    while (cin >> str1) {
        str2.push_back(str1);
    }
    for (int i = str2.size() - 1; i >= 0; i--) {
       cout << str2[i] << ' ';
    }
    return 0;
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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