1019. 数字黑洞

18 篇文章 0 订阅

这里写图片描述

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool big(int i, int j)
{
    return (i > j);
}

bool small(int i, int j)
{
    return (i < j);
}

int _tmain(int argc, _TCHAR* argv[])
{
    int num;
    cin >> num;
    do                                     //用do-while保证至少执行一次,防止输入为6174的情况没有输出
    {
        vector<int> num_temp;
        while (num)
        {
            int temp = num % 10;
            num_temp.push_back(temp);
            num /= 10;
        }
        while (num_temp.size()<4)                               //输入小于1000,则添零补齐四位,无所谓在哪补齐,后面还要排序
        {
            num_temp.push_back(0);
        }
        sort(num_temp.begin(), num_temp.end(), big);
        int big_num = 0;
        for (auto c : num_temp)
        {
            big_num = big_num * 10 + c;
        }
        sort(num_temp.begin(), num_temp.end(), small);
        int small_num = 0;
        for (auto c : num_temp)
        {
            small_num = small_num * 10 + c;
        }
        if (big_num==small_num)                               //如果输入的各位都相等,则输出0000
        {
            printf("%04d - %04d = 0000\n", big_num, big_num);                            //输出用printf可以直接补零,用cout还得判断情况手动补零
            break;
        }
        else
        {
            printf("%04d - %04d = %04d\n", big_num, small_num, big_num - small_num);
        }
        num = big_num - small_num;
    }while (num != 6174);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值