首页 > 其他 > 详细

1019 数字黑洞

时间:2020-02-16 14:02:43      阅读:67      评论:0      收藏:0      [点我收藏+]

注意:位数不够四位,就高位补0;

新学一个string函数。str.insert(pos,N,字符)表示在pos位置,插入N个字符。

#include"iostream"
#include<algorithm>
using namespace std;

bool cmp(char a,char b) {
    return a > b;
}

int main() {
    string a,b;
    cin>>a;
    a.insert(0, 4-a.length(), 0);//如果不足4位,就高位补零。 
    while(1) {
        b = a;
        sort(a.begin(),a.end(),cmp);//字符串中的字符递增排序
        sort(b.begin(),b.end());
        string result = to_string(stoi(a) - stoi(b));
        result.insert(0,4-result.length(),0); //如果不足4位,就高位补零。 
        printf("%s - %s = %s\n",a.c_str(),b.c_str(),result.c_str());
        if(result == "6174" || result == "0000")
            break;
        a = result;
    }
    return 0;
}

 

1019 数字黑洞

原文:https://www.cnblogs.com/keep23456/p/12316415.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!