华东师范考研机试:没用的知识

因为要准备复试机试就每天刷两道题

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条件,差不多。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值