首页 > 其他 > 详细

TOJ4168: Same Digits

时间:2017-12-15 11:03:09      阅读:222      评论:0      收藏:0      [点我收藏+]

4168: Same Digits 技术分享图片

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没有什么卵用

#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;
}

 

TOJ4168: Same Digits

原文:http://www.cnblogs.com/BobHuang/p/8041913.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!