A. Distinct Digits
滴答滴答---题目链接A. Distinct Digitstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have two integ...
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:
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 output Copy input Copy output Copy 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;
}
更多推荐
所有评论(0)