zoukankan      html  css  js  c++  java
  • Permute Digits

    C. Permute Digits
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given two positive integer numbers a and b. Permute (change order) of the digits of a to construct maximal number not exceeding b. No number in input and/or output can start with the digit 0.

    It is allowed to leave a as it is.

    Input

    The first line contains integer a (1 ≤ a ≤ 1018). The second line contains integer b (1 ≤ b ≤ 1018). Numbers don't have leading zeroes. It is guaranteed that answer exists.

    Output

    Print the maximum possible number that is a permutation of digits of a and is not greater than b. The answer can't have any leading zeroes. It is guaranteed that the answer exists.

    The number in the output should have exactly the same length as number a. It should be a permutation of digits of a.

    Examples
    input
    Copy
    123
    222
    output
    Copy
    213
    input
    Copy
    3921
    10000
    output
    Copy
    9321
    input
    Copy
    4940
    5000
    output
    Copy
    4940


    一道cf的c题,挺好的,直接用字符串的操作可以很快的解决.
     1 #include <iostream>
     2 #include <algorithm>
     3 #define ll long long int
     4 using namespace std;
     5 string s,ss;
     6 string st;
     7 int main(){
     8     cin>>s>>ss;
     9     sort(s.begin(),s.end());
    10     if(ss.length()>s.length()){
    11         reverse(s.begin(),s.end());
    12         cout<<s<<endl;
    13         return 0;
    14     }
    15     for(int i=0;i<s.length();i++)
    16         for(int j=s.length()-1;j>i;j--){
    17             st=s;
    18             swap(s[i],s[j]);
    19             sort(s.begin()+i+1,s.end());
    20             if(s.compare(ss)>0)
    21                 s=st;
    22             else break;
    23         }
    24     cout<<s<<endl;
    25     return 0;
    26 }
  • 相关阅读:
    boundandbranch method
    图像格式PPM,PGM和PBM
    感兴趣文章
    生成数据库脚本
    安徽太和华药会总结
    正则表达式语法参考
    xml
    对项目开发有很大帮助的jquery网站
    增强 VSS 的文件共享安全性
    支付宝及时到帐接口使用详解
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/9314589.html
Copyright © 2011-2022 走看看