L1-033 出生年 (15分)
以上是新浪微博中一奇葩贴:“我出生于 (1988) 年,直到 (25) 岁才遇到 (4) 个数字都不相同的年份。”也就是说,直到 (2013) 年才达到“(4) 个数字都不相同”的要求。本题请你根据要求,自动填充“我出生于 (y) 年,直到 (x) 岁才遇到 (n) 个数字都不相同的年份”这句话。
输入格式:
输入在一行中给出出生年份 (y) 和目标年份中不同数字的个数 (n),其中 (y) 在 ([1, 3000]) 之间,(n) 可以是 (2)、或 (3)、或 (4)。注意不足 (4) 位的年份要在前面补零,例如公元 (1) 年被认为是 (0001) 年,有 (2) 个不同的数字 (0) 和 (1)。
输出格式:
根据输入,输出 (x) 和能达到要求的年份。数字间以 (1) 个空格分隔,行首尾不得有多余空格。年份要按 (4) 位输出。注意:所谓“(n) 个数字都不相同”是指不同的数字正好是 (n) 个。如“(2013)”被视为满足“(4) 位数字都不同”的条件,但不被视为满足 (2) 位或 (3) 位数字不同的条件。
输入样例1:
1988 4
输出样例1:
25 2013
输入样例2:
1 2
输出样例2:
0 0001
代码:
#include<bits/stdc++.h>
using namespace std;
int y,n,tot;
bool vis[15];
int main()
{
cin>>y>>n;
for(int i=y;i<=9999;i++)
{
memset(vis,0,sizeof(vis));
tot=0;
for(int j=1;j<=1000;j*=10)
if(!vis[i/j%10])tot+=vis[i/j%10]=1;
if(tot==n)return cout<<i-y<<' '<<setw(4)<<setfill('0')<<i<<endl,0;
}
return 0;
}