PAT basic 1019

来源:互联网 发布:烈焰传奇翅膀进阶数据 编辑:程序博客网 时间:2024/06/11 07:19
#include<iostream>  //stio 是c++ 11的,所以工具 编译 代码生成 #include<algorithm>#include<string>using namespace std;bool cmp(char a,char b){    return a > b;} int main(){    string s;    cin >> s;    s.insert(0, 4 - s.length(), '0');    do    {        string a = s,b = s;        sort(a.begin(),a.end(),cmp);   //从大到小         sort(b.begin(),b.end()); //从小到大         int result = stoi(a) - stoi(b);        s = to_string(result);        s.insert(0, 4 - s.length(),'0'); // 用来给不足4位的时候前面补0        cout << a << " - " << b << " = " << s << endl;    }while(s != "6174" && s != "0000");    return 0;}