PAT:数字黑洞

题目链接:


https://www.patest.cn/contests/pat-b-practise/1019

#include 
#include
using namespace std;

class Solution{
public:
    static bool Cmp(char a,char b){
        return a>=b;
    }
    void getAndPrint(){
        string number;
        cin>>number;

        do{
            sort(number.begin(),number.end(),Cmp);

            if(number.size()<4) number.append(4-number.size(),'0');

            string revNum=number;
            reverse(revNum.begin(),revNum.end());
            cout<=0 && b_end>=0){
            int a_int=a[a_end]-'0',b_int=b[b_end]-'0';

            if(flag != 0){//向前一位借数
                if(a[a_end-1]-1 >= 0) flag--,a[a_end-1]--,a_int+=10;
                else a[a_end-1]=9,a_int+=10;
            }

            int subs=a_int-b_int;
            if(subs<0) {flag++;continue;}//被减数小于减数
            result.insert(result.begin(),subs+'0');
            a_end--;
            b_end--;
        }
        return result;
    }
};
int main(){
    Solution a;
    a.getAndPrint();
}


你可能感兴趣的:(算法学习)