问题 J: Palindromic Password ( 2018组队训练赛第十五场) (简单模拟)

问题 J: Palindromic Password

时间限制: 3 Sec  内存限制: 128 MB
提交: 217  解决: 62
[提交][状态][讨论版][命题人: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...

输入

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.

样例输入

2
123321
123322

样例输出

123321
123321


求出这个数的最接近的回文数

嗯 尽管这个时间很大 但是还是不能直接暴力的

判断回文数的时候其实它前后不一样有一个不一样我们就可以跳过了,并且范围从1e6枚举到1e7太呆了,而且肯定要超时的。
既然是最接近的回文数,那么在给出这个数的上下一个范围内一定可以找到,我们可以限制出这个范围(随意)。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int limit = 100000;
 5 int main()
 6 {
 7     int n;
 8     scanf("%d",&n);
 9     for(int i=0; i<n; i++)
10     {
11         int num;
12         scanf("%d",&num);
13         int ans;
14         int dis = 100001;
15         int r = num+limit;
16         int l = num>=200000?num-limit:limit;
17         for(int j=l; j<=r; j++)
18         {
19             int a = j%10;
20             int f = j/100000;
21             if(a == f)
22             {
23                 int b = j/10%10;
24                 int e = j/10000%10;
25                 if(b == e)
26                 {
27                     int c = j/100 % 10;
28                     int d = j/1000%10;
29                     if(c == d && abs(num-j) < dis)
30                     {
31                         dis = abs(num - j);
32                         ans = j;
33                     }
34                 }
35             }
36         }
37         printf("%d\n",ans);
38     }
39 }
View Code

猜你喜欢

转载自www.cnblogs.com/iwannabe/p/8951937.html
今日推荐