因为要准备复试机试就每天刷两道题
B - 没用的知识 CodeForces - 271A
冷知识:
是不是特别神奇?
掌握了这个知识我们可以做这个题目:给出一个1000年之后的某个年份,请你计算到"四个数字都不相同"的最近的一个年份.
Input
输入一行, 一个正整数y,表示年份.(1000 ≤ y ≤ 9000) .
Output
输出一行,一个年份,表示大于y并且四位数字都不相同的最小年份y
不难证明,本题中的解是一定存在的
Examples 1
Input
1987
Output
2013
Examples 2
Input
2013
Output
2014
我的代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<vector>
#include<cmath>
using namespace std;
int main()
{
int a;
cin>>a;
a++;
while(1)
{
string s=to_string(a);
if(s[0]!=s[1]&&s[0]!=s[2]&&s[0]!=s[3]&&s[1]!=s[2]&&s[1]!=s[3]&&s[2]!=s[3])
{
cout<<a<<endl;
return 0;
}
else
a++;
}
}
参考答案
#include <cstdio>
int isValid(int x)
{
char s[5];
for ( int i=0;i<=4;i++)
{
s[i] = x%10;
}
for(int i=0;i<4;i++)
{
for(int j=i+1;j<4;j++)
{
if(s[i]==s[j])
{
return 0;// not OK
}
}
}
return 1;
}
int main(void)
{
int y;
scanf("%d",&y);
y++;
while( !isValid(y) )
{
y++;
}
printf("%d\n",y);
return 0;
}
反思
忽略掉不美观的if条件,差不多。