题目描述

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
两行代码就搞定了

本文提供了两种不同的字符串反转方法,一种使用C++,包括两种解法,另一种使用Python,仅需两行代码即可完成。C++解法涉及字符串处理和数组操作,而Python则利用了其内置的字符串反转功能。
649

被折叠的 条评论
为什么被折叠?



