CF271 A. Beautiful Year
February 15, 2013
CodeForces
A. Beautiful Year time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output
It seems like the year of 2013 came only yesterday. Do you know a curious fact? The year of 2013 is the first year after the old 1987 with only distinct digits.
Now you are suggested to solve the following problem: given a year number, find the minimum year number which is strictly larger than the given one and has only distinct digits. Input
The single line contains integer y (1000 ≤ y ≤ 9000) — the year number. Output
Print a single integer — the minimum year number that is strictly larger than y and all it’s digits are distinct. It is guaranteed that the answer exists. Sample test(s) input
1987
output
2013
input
2013
output
2014
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
int judge(int n)
{
int a[10];
memset(a, 0, sizeof(a));
int k = 1;
for (int i = 0; i < 4; ++i)
{
if (a[(n/k)%10] == 0)
{
a[(n/k)%10] = 1;
}
else return 0;
k *= 10;
}
return 1;
}
int main(void)
{
int y;
while (~scanf("%d", &y))
{
for (int i = 1; ; ++i)
{
if (judge(y+i))
{
printf("%d\n", y+i); break;
}
}
}
return 0;
}
天下没有水题……
先看清题目意思,然后认真快速敲代码就可以了。
意思是给出一个年份,求出严格比它大的,并且这个年份的四个数字互不相同的最小年份,年份的范围是[1000, 9000]