提交: 210 解决: 58
[ 提交][ 状态][ 讨论版][命题人: admin]
题目描述
The IT department at your school decided to change their password policy. Each password will have to
consist of N 6-digit numbers separated by dashes, where N will be determined by the phase of the moon
and the weather forecast for the day after it will be generated.
You realized that, if all of the numbers were palindromes (same numbers as the original ones if read backwards), you would have to remember a bunch of 3-digit numbers, which did not sound that bad (at the time).
In order to generate your password of N numbers, you get a list of N randomly generated 6-digit numbers and find the palindromic number closest to them.
Of course, you would like to automate this process...
You realized that, if all of the numbers were palindromes (same numbers as the original ones if read backwards), you would have to remember a bunch of 3-digit numbers, which did not sound that bad (at the time).
In order to generate your password of N numbers, you get a list of N randomly generated 6-digit numbers and find the palindromic number closest to them.
Of course, you would like to automate this process...
输入
The first line of the input contains a single positive integer N≤1000 indicating the number of six-digit numbers in the input. Each of the next N lines contains a six-digit number without leading zeroes.
输出
For each six-digit number in the input, output another six-digit number that is closest to it and is also a
palindrome. “Closest” in this context means “a number having the smallest absolute difference with the
original number”. If there are two different numbers satisfying the above condition, output the smaller one
of the two. Remember, no leading zeroes.
of the two. Remember, no leading zeroes.
样例输入
2
123321
123322
样例输出
123321
123321
题意:输入n个实例,每个实例是一个六位数,输出与他差值最小的六位回文数,如果有两个,输出较小的那一个。
分析:打一个六位回文数的表,有900个回文数,然后循环查找与输入数字最接近的回文数,注意输出小的。
#include<cstdio>
#include<cstring>
#include<math.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<iostream>
#include<map>
#define mes(a,b) memset(a,b,sizeof(a))
#define rep(i,m,n) for(i=m;i<=n;i++)
typedef long long ll;
using namespace std;
int max3(int a,int b,int c)
{
return max(max(a,b),c);
}
ll min3(ll a,ll b,ll c)
{
return min(min(a,b),c);
}
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int mod=1e9+7;
int dir[4][2]= {0,1,1,0,0,-1,-1,0};
ll gcd(ll a,ll b)
{
return b?gcd(b,a%b):a;
}
ll lcm(ll a,ll b)
{
return a/gcd(a,b)*b;
}
ll inv(ll b)
{
if(b==1)return 1;
return (mod-mod/b)*inv(mod%b)%mod;
}
ll fpow(ll n,ll k)
{
ll r=1;
for(; k; k>>=1)
{
if(k&1)r=r*n%mod;
n=n*n%mod;
}
return r;
}
ll Fpow(ll n,ll k)
{
ll r=1;
for(; k; k>>=1)
{
if(k&1)r=r*n;
n=n*n;
}
return r;
}
int a[1000];
int i,j=0;
void huiwenshu()//打表六位数的回文表
{
for(i=100000; i<1000000; i++)
{
if(i/100000==i%10&&i/10000%10==i/10%10&&i/1000%10==i/100%10)
a[j++]=i;
}
}
int main()
{
int n;
int t;
scanf("%d",&t);
huiwenshu();
while(t--)
{
scanf("%d",&n);
if(n==100000)//回文表第一个数位100001,大于100000
{
printf("100001\n");
}
else
{
for(i=0; i<j; i++)
{
if(a[i]==n)
{
printf("%d\n",a[i]);
break;
}
else if(a[i]<n&&a[i+1]>n)
{
printf("%d\n",n-a[i]>a[i+1]-n?a[i+1]:a[i]);//注意相等时输出较小的那一个
break;
}
}
}
}
return 0;
}