1.17第三题

C - 3

Time limit 2000 ms
Memory limit 262144 kB

Problem Description

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 Input

1987

2013

Sample Output

2013

2014

问题链接:CodeForces - 271A

问题简述:

输入一个数,找出一个比他大的数,要求每个数位都不能相同且为最小的数

问题分析:

题目限定1000~9999,即只有四位数,可以把每个数位提取出来,利用循环递增找出目标

程序说明:

break用于结束循环

AC通过的C语言程序如下:

#include <iostream>
using namespace std;

int main()
{
	int y;
	cin >> y;
	int a, b, c, d;
	a = (y / 1000) % 10;
	b = (y / 100) % 10;
	c = (y / 10) % 10;
	d = (y / 1) % 10;
	int a1, b1, c1, d1;
	for (int i = y+1;i < 9500;i++)
	{
		a1 = (i / 1000) % 10;
		b1 = (i / 100) % 10;
		c1 = (i / 10) % 10;
		d1 = (i / 1) % 10;
		if ((a1 != b1) && (a1 != c1) && (a1 != d1) && (b1 != c1) && (b1 != d1) && (c1 != d1))
		{
			cout << i;break;
		}
	}
 

猜你喜欢

转载自blog.csdn.net/weixin_44003969/article/details/86551119