题目描述
C++解法
解法1
#include<cstdio>
#include<cstring>
using namespace std;
#include<stdio.h>
#include<iostream>
int main(){
char str[90];
cin.getline(str,90);
int len=strlen(str),r=0,h=0;
char ans[90][90];
for(int i=0;i<len;i++){
if(str[i]!=' '){
ans[r][h++]=str[i];
}else{
ans[r][h]='\0';
r++;
h=0;
}
}
for(int i=r;i>=0;i--){
printf("%s",ans[i]);
if(i>0) printf(" ");
}
return 0;
}
解法2
#include <iostream>
#include <stack>
using namespace std;
int main() {
stack<string> v;
string s;
while(cin >> s) v.push(s);
cout << v.top();
v.pop();
while(!v.empty()) {
cout << " " << v.top();
v.pop();
}
return 0;
}
python解法
l = input().split()
print(' '.join(l[::-1]))
使以字符串反转还是要用Python
两行代码就搞定了