TOJ4168: Same Digits

4168: Same Digits 分享至QQ空间

Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByte
Total Submit: 115            Accepted:62

Description

 

Your program will be given an integer X. Find the smallest number larger than X consisting of the same digits as X.

 

Input

 

The first line of input contains the integer X (1 ≤ X ≤ 999 999).
The first digit in X will not be a zero.

 

Output

 

Output the result on a single line. If there is no such number, output 0.

 

Sample Input

 156

Sample Output

 165

这个前缀为0没有什么卵用

贪心去实现,是不是存在,就看他后面是不是有比他大的

然后找到最新奥德那个数交换后直接sort

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
    char s[10];
    gets(s);
    int n=strlen(s),i,j;
    for(i=n-1; i>0; i--)
        if (s[i]>s[i-1])break;
    if(!i)puts("0");
    else
    {
        int x=s[i-1],f=i;
        for (j=i+1; j<n; j++)
            if(s[j]>x&&s[j]<s[f])f=j;
        swap(s[f], s[i-1]);
        sort(s+i,s+n);
        puts(s);
    }
    return 0;
}

 

posted @ 2017-12-15 10:55  暴力都不会的蒟蒻  阅读(300)  评论(0编辑  收藏  举报