描述
输入一个整数,将这个整数以字符串的形式逆序输出
程序不考虑负数的情况,若数字含有0,则逆序形式也含有0,如输入为100,则输出为001
数据范围
: 0 \le n \le 2^{30}-1 \0≤n≤2
30
−1
输入描述
:
输入一个int整数
输出描述
:
将这个整数以字符串的形式逆序输出
使用迭代器1
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
int x=0;
cin>>x;
string str=to_string(x);//将x转为字符串
cout<<string(str.rbegin(),str.rend());//通过字符串迭代器逆序生成一个新的字符串作为结果输出
return 0;
}
使用迭代器2
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
int x=0;
cin>>x;
string str=to_string(x);//将x转为字符串
for(auto c=str.rbegin();c<str.rend();c++){
cout<<*c;
}
}
使用algorithm的reverse()
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
int x=0;
cin>>x;
string str=to_string(x);//将x转为字符串
reverse(str.begin(),str.end());
cout<<str;
return 0;
}
使用队列
#include <iostream>
#include <string>
#include <queue>
using namespace std;
int main() {
int x=0;
cin>>x;
if(x==0)
{
cout<<x;
return 0;
}
queue<int> si;
while(x){
si.push(x%10);
x/=10;
}
while(!si.empty()){
cout<<si.front();
si.pop();
}
return 0;
}