You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.5 KiB

2 years ago
#include<bits/stdc++.h>
using namespace std;
const int N = 10;
int a[N], cnt;
//求反数:数位分离数字,然后想办法再级成大数字
void FanShu_ShuWeiFenLi(int x) {
int t = x;
//数位分离到一个数组中
//12345--->[5,4,3,2,1]=5*10000+4*1000+3*100+2*10+1*1=54321
while (t) {
int m = t % 10;
a[cnt++]= m;
t /= 10;
}
//打印出这个数组
for (int i = 0; i < cnt; i++)cout << a[i];
//方法1通过数学公式pow拼成一个大数字
int ans = 0;
//5,4,3,2,1=5*10000
//10^4
//cnt=5
//cnt-1
//cnt-i-1
for (int i = 0; i < cnt; i++)
ans += a[i] * pow(10, cnt - i - 1);
//分析一下为什么是cnt-i-1
cout << ans << endl;
//方法2):利用频繁的累乘方式拼成一个大数字
ans = 0;//12345-->54321
//5*10=50--+4>54
//54*10=540+3=543
//543*10=5430+2=5432
//5432*10+1=54321
for (int i = 0; i < cnt; i++)
ans = ans * 10 + a[i];
cout << ans << endl;
//方法3:利用字符串转换
string s = "";
for (int i = 0; i < cnt; i++) s += to_string(a[i]);
cout << s << endl;
}
//利用原始字符串反转进行
void FanShu_ByString(int x) {
string a = to_string(x);
reverse(a.begin(), a.end());
cout << a << endl;
}
int main() {
//测试一个数字
FanShu_ShuWeiFenLi(12345);
//利用原始字符串反转进行
FanShu_ByString(12345);
return 0;
}