数字颠倒
输入一个整数,将这个整数以字符串的形式逆序输出
(程序不考虑负数的情况,若数字含有0,则逆序形式也含有0,如输入为100,则输出为001)
方法一:
使用字符串接收颠倒后的数字,然后打印字符串
#include<bits/stdc++.h>
using namespace std;
int main()
{
int num;
cin >> num;
string str;
while(num)
{
int temp;
temp=num%10;
str+=(char)(temp+'0');
num/=10;
}
cout << str << endl;
return 0;
}
方法二:
直接打印颠倒后的字符串
#include<bits/stdc++.h>
using namespace std;
int main()
{
int num;
cin >> num;
string str;
while(num)
{
int temp;
temp=num%10;
cout<<temp;
num/=10;
}
cout << str << endl;
return 0;
}