ACM寒假集训——遍历每一位

You have two integers l and r. Find an integer x which satisfies the conditions below:

l≤x≤r.
All digits of x are different.
If there are multiple answers, print any of them.

Input
The first line contains two integers l and r (1≤l≤r≤105).

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

Examples
Input
121 130
Output
123
Input
98766 100000
Output
-1
Note
In the first example, 123 is one of the possible answers. However, 121 can’t be the answer, because there are multiple 1s on different digits.

In the second example, there is no valid answer.

题目思路:本题思路是将l到r之间的每个数都遍历,在遍历的同时判断他们是否符合要求,符合之间输出,不符合输出-1.在遍历一个数的时候,用一个数组的下标存放他们的每一位,并且在存放时此位置加一,如果有下标大于等于2则不符合要求。

ac代码:

#include<stdio.h>
#include
#include
using namespace std;
int main()
{
int l,r,t,flage,dp[15];
scanf("%d%d",&l,&r);
for(int i=l;i<=r;i++)
{
t=i,flage=0;
memset(dp,0,sizeof(dp));
while(t)
{
dp[t%10]++;
if(dp[t%10]>=2)
{
flage=1;
break;
}
t/=10;
}
if(!flage) return !printf("%d\n",i);
}
return !printf("-1\n");
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值