1019 数字黑洞

在这里插入图片描述
在这里插入图片描述

#include <iostream>
#include<algorithm>
using namespace std;
int turn(int n)
{
    int q1=n%10;
    int q2=(n%100)/10;
    int q3=(n%1000)/100;
    int q4=n/1000;
    return q4+q3*10+q2*100+q1*1000;
}
int f(int n)
{
    int flag=2;
    int q1=n%10;
    int q2=(n%100)/10;
    int q3=(n%1000)/100;
    int q4=n/1000;
    if(q1==q2&&q1==q3&&q1==q4)
    {
        flag=1;
    }
    return flag;
}
int ver(int n)
{
    int q[4];
    q[0]=n%10;
    q[1]=(n%100)/10;
    q[2]=(n%1000)/100;
    q[3]=n/1000;
    sort(q,q+4);
    return q[0]+q[1]*10+q[2]*100+q[3]*1000;
}
int main()
{
    int n;
    cin>>n;
    n=ver(n);
    int t=1;
    if(f(n)==1)
    {
        cout<<n%10<<n%10<<n%10<<n%10<<" - "<<n%10<<n%10<<n%10<<n%10<<" = 0000";
    }
    else
    {
        while(t!=6174)
        {
            t=n-turn(n);
            if(turn(n)<1000&&turn(n>=100))
            {
                cout<<n<<" - 0"<<turn(n)<<" = "<<t<<endl;
            }
            else if(turn(n)<100&&turn(n>=10))
            {
                cout<<n<<" - 00"<<turn(n)<<" = "<<t<<endl;
            }
            else if(turn(n)<10)
            {
                cout<<n<<" - 000"<<turn(n)<<" = "<<t<<endl;
            }
            else
            {
                cout<<n<<" - "<<turn(n)<<" = "<<t<<endl;
            }
            n=ver(t);
        }
    }
    return 0;
}

有错
在这里插入图片描述

#include <iostream>
#include <algorithm>

using namespace std;

int compare(const int &a, const int &b){
    return a>b;
}

int main(){
    int n;
    scanf("%d", &n);
    if(n % 1111 == 0){
        printf("%d - %d = 0000\n", n, n);
        return 0;
    }
    else{
        int tmp = n;
        while(true){
            char c[5] = {'0','0','0','0','\0'};
            int a, b;
            for(int i = 3; tmp != 0; i--){
                c[i] = (tmp % 10) + 48;
                tmp /= 10;
            }
            sort(c, c + 4, compare);
            a = (c[0] - 48) * 1000 + (c[1] - 48) * 100 + (c[2] - 48) * 10 + (c[3] - 48);
            printf("%s - ", c);
            sort(c, c + 4);
            b = (c[0] - 48) * 1000 + (c[1] - 48) * 100 + (c[2] - 48) * 10 + (c[3] - 48);
            printf("%s = ", c);
            tmp = a - b;
            printf("%04d\n", tmp);
            if(tmp == 6174){
                break;
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值