1069 The Black Hole of Numbers (20 分)

时间:2021-02-15 12:21:49   收藏:0   阅读:0

注意不满\(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\n",a,b,res);
            S.insert(res);
            s=to_string(res);
            while(s.size()<4) s=‘0‘+s;
        }
    }
    //system("pause");
    return 0;
}
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!