Palindromic Password

Time Limit:  3 Sec   Memory Limit:  128 MB
Commits:  210   Resolved:  58
[ Commit ][ Status ][ Discussion Board ][Assert By: admin ]

Topic description

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...

enter

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.

output

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.

sample input

2
123321

123322

Sample output

123321
123321

The meaning of the question: input n instances, each instance is a six-digit number, output the six-digit palindrome number with the smallest difference with him, if there are two, output the smaller one.

Analysis: Make a six-digit palindrome number table, there are 900 palindrome numbers, and then loop to find the palindrome number closest to the input number, pay attention to the small output.

#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;
}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324846857&siteId=291194637