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.
아 그래 A번ㅋㅋ 이거 풀면서 분명 더 쉽게 풀 수 있을 것 같은데?? 이게 최선인가?? 하면서 풀었음. 풀 줄은 알겠는데... 알겠는데...... 괜찮은 방법이 생각 안 나서 걍 풀었음. 코드 보면 오 덜티! 하면서 기함할듯 (내가)
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <string>
using namespace std;
using i64 = long long;
int main() {
int l, r;
scanf("%d %d", &l, &r);
int i;
for(i = l; i <= r; i++){
if(i == 100000){
continue;
}
else if (i >= 10000){
int a=i/10000,b=i%10000/1000,c=i%1000/100,d=i%100/10,e=i%10;
if(a==b || a==c || a==d || a==e || b==c || b==d || b==e || c==d || c==e || d==e)
continue;
else
break;
}
else if (i >= 1000){
int b=i/1000,c=i%1000/100,d=i%100/10,e=i%10;
if(b==c || b==d || b==e || c==d || c==e || d==e)
continue;
else
break;
}
else if (i >= 100){
int c=i%1000/100,d=i%100/10,e=i%10;
if( c==d || c==e || d==e)
continue;
else
break;
}
else if (i >= 10){
int d=i/10,e=i%10;
if(d==e)
continue;
else
break;
}
else{
break;
}
}
if(i == r+1){
printf("-1");
}
else
printf("%d", i);
return 0;
}
ㅋㅋㅎㅠ
어쩔 수 없다
이 방법 밖에 안 떠올랐음
다시 좀 더 좋은 방법이 없나 고민하다가 unique랑 erase썼던게 생각났다.
이 두 개 쓰면 중복된 값이 있을 시 배열 크기가 줄어든다. 그래서 이전 크기 저장해두고 이후 크기랑 비교하면 좀 더 깔끔히 구할 수 있겠다 싶었음.
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <string>
using namespace std;
using i64 = long long;
int main() {
int l, r;
scanf("%d %d", &l, &r);
for(int i = l; i <= r; i++){
string s = to_string(i);
int s_size = s.size();
sort(s.begin(), s.end());
s.erase(unique(s.begin(), s.end()), s.end());
if(s_size == s.size()){
printf("%d\n", i);
return 0;
}
}
printf("-1\n");
return 0;
}
예헤이~
이렇게 해도 맞았음
문제 풀 때 왜 이 방법이 안 떠올랐을까
'코드포스' 카테고리의 다른 글
[코드포스 Practice10] C. Video Posts (0) | 2020.01.09 |
---|---|
[코드포스 Practice10] B. Mislove Has Lost an Array (0) | 2020.01.09 |
[코드포스 Practice10] 후기 (0) | 2020.01.09 |
[코드포스 Practice9] E. RGB Substring (hard version) (0) | 2020.01.04 |
[코드포스 Practice9] D. Minimum number of steps (0) | 2020.01.04 |