A. Distinct Digits

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You have two integers ll and rr. Find an integer xx which satisfies the conditions below:

  • l≤x≤rl≤x≤r.
  • All digits of xx are different.

If there are multiple answers, print any of them.

Input

The first line contains two integers ll and rr (1≤l≤r≤1051≤l≤r≤105).

Output

If an answer exists, print any of them. Otherwise, print −1−1.

Examples

input

Copy

121 130

output

Copy

123

input

Copy

98766 100000

output

Copy

-1

Note

In the first example, 123123 is one of the possible answers. However, 121121 can't be the answer, because there are multiple 11s on different digits.

In the second example, there is no valid answer.

解题说明:水题,从l到r进行遍历,然后判断0-9这些数字是否只出现一次,满足条件即退出循环。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

int main()
{
	int l, r, f, i, j, g = 0;
	int a[10] = { 0 };
	scanf("%d%d", &l, &r);
	for (i = l; i <= r; i++)
	{
		f = 1;
		for (j = i; j > 0; j = j / 10)
		{
			if (a[j % 10] == 1)
			{
				f = 0;
				break;
			}
			else
				a[j % 10] = 1;
		}
		if (f == 1)
		{
			printf("%d\n", i);
			g = 1;
			break;
		}
	}
	if (g == 0)
	{
		printf("-1\n");
	}
	return 0;
}
发布了1729 篇原创文章 · 获赞 371 · 访问量 273万+

猜你喜欢

转载自blog.csdn.net/jj12345jj198999/article/details/102642786
今日推荐