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

TOJ4168: Same Digits

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

标签:gets   ted   ber   size   class   panel   mem   3.1   java   

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

标签:gets   ted   ber   size   class   panel   mem   3.1   java   

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

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