zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    分布式事务--AT+TCC
    Java基础面试题
    JVM问题
    集合问题
    线程问题
    微服务面试题
    【入职准备】安装STS以及整合maven
    事务----四大特性
    html小知识--创建表单
    通过css润色html表格
  • 原文地址:https://www.cnblogs.com/BobHuang/p/8041913.html
Copyright © 2011-2022 走看看