题解 | #句子逆序#
句子逆序
https://www.nowcoder.com/practice/48b3cb4e3c694d9da5526e6255bb73c3
#include<bits/stdc++.h>
using namespace std;
int main() {
string str, res;
while (cin >> str) {
str += " " + res;
res = str;
}
cout << res << endl;
return 0;
}
首先这个代码并不能通过多组输入多组输出的情况,但oj能过;
下面给出一个我自己的暴力解法。。。纯暴力的那种。。
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
string s;
while (getline(cin, s)) {
string a[1000];
int j = 0;
for (int i = 0; i < s.length(); i++) {
if (s[i] == ' '){
j++;
}
else a[j] += s[i];
}
for (int i = j; i >= 0; i--) {
cout << a[i] << " ";
}
}
}
小草神的编程日记 文章被收录于专栏
学习编程两年半,希望大家多多关照指点
深信服公司福利 790人发布