问题 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

 

转载于:https://www.cnblogs.com/iwannabe/p/8951937.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值