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.

87 lines
2.1 KiB

2 years ago
#include<bits/stdc++.h>
using namespace std;
const int N = 110;
int a[N], cnt ;
//求反数:数位分离数字,然后想办法再级成大数字
bool HWS_1(int x) {
//注意:反复使用不重新初始化肯定要出问题!!!!!
//初始化
memset(a, 0, sizeof a);
cnt = 0;
int t = x;
//数位分离到一个数组中
while (t) {
int m = t % 10;
a[cnt++] = m;
t /= 10;
}
//方法1通过数学公式POW拼成一个大数字
int ans = 0;
for (int i = 0; i < cnt; i++) ans += a[i] * pow(10, cnt - i - 1);//分析一下为什么是cnt-i-1
return ans == x;
}
bool HWS_2(int x) {
//注意:反复使用不重新初始化肯定要出问题!!!!!
//初始化
memset(a, 0, sizeof a);
cnt = 0;
int t = x;
//数位分离到一个数组中
while (t) {
int m = t % 10;
a[cnt++] = m;
t /= 10;
}
//方法2):利用频繁的累乘方式拼成一个大数字
int ans = 0;
for (int i = 0; i < cnt; i++) ans = ans * 10 + a[i];
return ans == x;
}
bool HWS_3(int x) {
//注意:反复使用不重新初始化肯定要出问题!!!!!
//初始化
memset(a, 0, sizeof a);
cnt = 0;
int t = x;
//数位分离到一个数组中
while (t) {
int m = t % 10;
a[cnt++] = m;
t /= 10;
}
//方法3:利用字符串转换
string s = "";
for (int i = 0; i < cnt; i++) s += to_string(a[i]);
return to_string(x) == s;
}
//利用原始字符串反转进行
bool HWS_4(int x) {
string a = to_string(x);
reverse(a.begin(), a.end());
return a == to_string(x);
}
int main() {
cout << HWS_1(12345) << endl;
cout << HWS_2(12345) << endl;
cout << HWS_3(12345) << endl;
cout << HWS_4(12345) << endl;
cout << endl;
cout << HWS_1(12321) << endl;
cout << HWS_2(12321) << endl;
cout << HWS_3(12321) << endl;
cout << HWS_4(12321) << endl;
return 0;
}