码迷,mamicode.com
首页 > 其他好文 > 详细

看个人思路吧,清晰的话就简单 CodeForces 271A - Beautiful Year

时间:2014-05-24 21:52:34      阅读:320      评论:0      收藏:0      [点我收藏+]

标签:acm   c   编程   codeforces   

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.

Sample test(s)
input
1987
output
2013
input
2013
output
2014
题意就是说给你一个年份,当然是四位数的,然后要你找出比这个数大且是最先打到四位数每个数字都不一样,可能有些同志英语不好看不懂然后小弟语文差表述也有问题,这样说吧:2013是给定的数字,比2013大且最先达到每个数字都不一样就是2014   2    0    1    4;现在应该能明白了吧?
那咱们现在就来讲讲思路:
定义一个year作为输入,然后就是while(1)的一个循环,再定义一个flag作为标记,初值为0;
然后就是取各个数位上的数字分别赋值给a,b,c,d,然后就是一个if比较,如果不一样则flag=1;
然后判断flag是否为一,为一代表已找到符合条件的数字,则break;
这里有一点注意的是:输入year之后一定要加一,不然像2013这样的是会输出2013滴!!!!!!切记,好啦,讲完!!!!!!
贴下代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int main()
{
    int i,j,k;
    int t,n,m;
    int a,b,c,d;
    int year,flag;
    while(scanf("%d",&year)!=EOF)
    {
        flag=0;
        //printf("%d %d %d %d\n",a,b,c,d);
        while(1)
        {
            year++;    //千万注意这里!!!!!!千万千万千千万!!!!!!
            d=year%10;
            c=(year/10)%10;
            b=(year/100)%10;
            a=(year/1000);
            if(a!=b&&a!=c&&a!=d&&b!=c&&b!=d&&c!=d)
            {
                flag=1;
            }
            if(flag==1)
                break;

        }
        printf("%d\n",year);
    }
    return 0;
}

如有发现bug,就指出来吧!!!!!!不胜感激!!!!!!

看个人思路吧,清晰的话就简单 CodeForces 271A - Beautiful Year,布布扣,bubuko.com

看个人思路吧,清晰的话就简单 CodeForces 271A - Beautiful Year

标签:acm   c   编程   codeforces   

原文地址:http://blog.csdn.net/u014231159/article/details/26774087

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!