-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBeautifulYear.cpp
80 lines (60 loc) · 1.76 KB
/
BeautifulYear.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* It seems like the year of 2013 came only yesterday. Do you know a curious fact?
The year of 2013 is the first year after the old 1987 with only distinct digits.
Now you are suggested to solve the following problem: given a year number,
find the minimum year number which is strictly larger than the given one and has only distinct digits.
Input
The single line contains integer y (1000 ≤ y ≤ 9000) — the year number.
Output
Print a single integer — the minimum year number that is strictly larger than y and all it's digits are distinct.
It is guaranteed that the answer exists.
*/
#include <iostream>
#include <string>
#include <algorithm>
#include<sstream>
using namespace std;
int main()
{
int next, i, j, numCheck, numCheck2;
string year, check;
cin >> year;
for(i = 0; i < year.size(); i++){
for(j = 0; j < i; j++){
if(year[i] == year[j]){
break;
}
}
if(j == i){
check = check + year[i];
}
}
numCheck = stoi(year);
numCheck2 = stoi(check);
if(numCheck == numCheck2){
check = "";
}
for(int k = 0; k < 9000; k++){
if(check.size() < year.size() ){
check = "";
next = stoi(year);
next = next + 1;
stringstream ss;
ss<<next;
ss>>year;
for(i = 0; i < year.size(); i++){
for(j = 0; j < i; j++){
if(year[i] == year[j]){
break;
}
}
if(j == i){
check = check + year[i];
}
}
}else{
next = stoi(year);
}
}
cout << next << endl;
return 0;
}