zoukankan      html  css  js  c++  java
  • 1069 The Black Hole of Numbers (20 分)

    注意不满(4)位的话要补成(4)位。

    string s;
    set<int> S;
    
    int main()
    {
        cin>>s;
        while(s.size()<4) s='0'+s;
    
        while(true)
        {
            sort(s.begin(),s.end(),greater<char>());
            string sa=s;
    
            reverse(s.begin(),s.end());
            string sb=s;
    
            int a=stoi(sa),b=stoi(sb);
            int res=a-b;
    
            if(S.count(res)) break;
            else
            {
                printf("%04d - %04d = %04d
    ",a,b,res);
                S.insert(res);
                s=to_string(res);
                while(s.size()<4) s='0'+s;
            }
        }
        //system("pause");
        return 0;
    }
    

    (update)
    用数组的写法。

    int a[5];
    int n;
    
    void get(int x)
    {
        for(int i=3;i>=0;i--)
            a[i]=x%10,x/=10;
    }
    
    int calc()
    {
        int res=0;
        for(int i=0;i<4;i++)
            res=res*10+a[i];
        return res;
    }
    
    int main()
    {
        cin>>n;
        while(true)
        {
            get(n);
            sort(a,a+4,greater<int>());
            int maxv=calc();
            reverse(a,a+4);
            int minv=calc();
    
            n=maxv-minv;
            printf("%04d - %04d = %04d
    ",maxv,minv,n);
            if(n == 0 || n == 6174) break;
        }
        //system("pause");
        return 0;
    }
    
  • 相关阅读:
    Python安装appium 遇见的报错
    appium
    QQ邮箱/微信邮箱发送邮件
    Python-变量
    神秘的咒语
    宿命的PSS
    E. Congruence Equation
    D. Substring
    leetcode 761. Special Binary String
    F
  • 原文地址:https://www.cnblogs.com/fxh0707/p/14397724.html
Copyright © 2011-2022 走看看