题目链接:点击打开链接
【题目大意】:给一串长度为6的密码,找与其差绝对值最小的回文数,若有两种答案,输出小的。
【思路】直接打表找出所有6位的回文数,代码如下。
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int main()
{
int num[910];
int cnt = 0;
int n, a;
for(int i=100; i<1000; i++) {
int x1 = i%10;
int x2 = i/10%10;
int x3 = i/100;
num[cnt++] = i*1000+x1*100+x2*10+x3;
}
cin >> n;
while(n --){
cin >> a;
int ans = 0, id;
for(int i=0; i<cnt; i++){
if(num[i] == a) {
ans = num[i];
break;
}
else if(num[i] > a) {
id = i;
break;
}
}
if(ans) cout << ans << endl;
else {
int tmp1 = num[id]-a;
int tmp2 = a-num[id-1];
if(tmp1 < tmp2)
cout << num[id] << endl;
else
cout << num[id-1] << endl;
}
}
return 0;
}