滴答滴答---题目链接 

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.

#include <iostream>
#include<stdio.h>
#include<map>
using namespace std;
map<int,bool>p;
int main()
{
    int l,r;
    cin>>l>>r;
    int k[12];
    int t=0;
    int flas=0;
    if(l%10==9)
        l+=1;
    int p1=l;
    while(p1)
    {
        k[t++]=p1%10;
        p1=p1/10;
    }
    int ans=0;
    for(int i=t-1; i>=0; i--)
    {
        flas=0;
        if(p[k[i]]==0)
        {
            p[k[i]]=1;
            ans=ans*10+k[i];
        }
        else
        {
            for(int j=k[i]; j<=9; j++)
            {
                if(p[j]==0)
                {
                    int s=ans*10+j;
                    if(s>r)break;
                    flas=1;
                    p[j]=1;
                    ans=ans*10+j;
                    break;
                }
            }
            if(flas==0)
            {

                for(int j=0; j<=9; j++)
                {
                    if(p[j]==0)
                    {
                        //cout<<j<<endl;
                        flas=1;
                        p[j]=1;
                        ans=ans*10+j;
                        break;
                    }
                }
            }
        }
    }
    //cout<<ans<<endl;
    if(ans<=r&&ans>=l)
    {
        printf("%d\n",ans);
    }
    else
    {
        printf("-1\n");
    }
    return 0;
}

做法二: 

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int l,r,k,m,n;
    cin>>l>>r;
    for(int i=l; i<=r; i++)
    {
        int a[10]= {0};
        m=0;
        k=i;
        while(k>0)
        {
            a[k%10]++;
            if(a[k%10]>1)
            {
                m=1;
                break;
            }
            k/=10;
        }
        if(m==0)
        {
            cout<<i;
            break;
        }
    }
    if(m==1)
    {
        cout<<"-1";
    }
    return 0;
}

 

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐