1019

作者: 峡迩 | 来源:发表于2017-07-23 00:51 被阅读0次
    #include<iostream>
    #include<string>
    #include<vector>
    #include<iomanip>
    #include<algorithm>
    
    
    using namespace std;
    
    inline int str_to_min(int a)
    {
        string tmp = to_string(a);
        while (tmp.size() < 4)
        {
            tmp = tmp + "0";
        }
    
        sort(tmp.begin(), tmp.end());
        unsigned ret = stoi(tmp);
        return ret;
    }
    inline int str_to_max(int a)
    {
        string tmp = to_string(a);
        while (tmp.size() <4)
        {
            tmp = tmp + "0";
        }
    
        sort(tmp.begin(), tmp.end(), [](char chr1, char chr2) {return chr1>chr2; });
        unsigned ret = stoi(tmp);
        return ret;
    }
    
    int main()
    {
        int n;
        cin >> n;
    
        int max_tmp = str_to_max(n);
        int min_tmp = str_to_min(n);
        int r = max_tmp -min_tmp ;
        if (r == 0)
        {
            cout << n << " - " << n << " = 0000";
        }
        else
        {
            cout.setf(ios::right);
            cout.fill('0');
            while (r!=6174)
            {
                cout <<setw(4)<< max_tmp << " - " << setw(4)<< min_tmp <<" = "<< setw(4)<< r<<endl;
                n = r;
                max_tmp = str_to_max(n);
                min_tmp = str_to_min(n);
                r = max_tmp - min_tmp;
                if (r==0)
                {
                    cout << setw(4) << max_tmp << " - " << setw(4) << min_tmp << " = " << "0000" ;
                    return 0;
                }
            }
            cout << setw(4)<< max_tmp << " - " << setw(4)<< min_tmp << " = " << setw(4)<< r;
        }
    
    
        cout << endl;
        system("pause");
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:1019

          本文链接:https://www.haomeiwen.com/subject/ffepkxtx.html