思路:
一開始的思路是把每個位上的數字單獨取出,再一一比較,看看到底有多少個不同。但這樣很複雜,寫起來很繁瑣。完全沒有想起set。。。。運用上set的話,就簡單多了,直接是數元素,裏面不會有重複的元素。
還有一個注意的地方是,輸出的年份要是四位數,不夠前面要補零。
#include<iostream>
#include<set>
#include<stdio.h>
using namespace std;
int main()
{
int x, y;
while (cin >> x >> y)
{
int i = x;
while (1)
{
set<int> s;
int z = i, j = 4;
while (j--)//完美解決了數字前面補零的問題
{
s.insert(z % 10);
z /= 10;
}
if (s.size() == y)
{
printf("%d %04d\n", i - x, i);
break;
}
i++;
}
}
system("pause");
return 0;
}