栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

1019 数字黑洞 (20 分)--每日一练

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

1019 数字黑洞 (20 分)--每日一练

#include 
int main(void)
{
    int num, count, time, max, min;
    int str[4];
    scanf("%d", &num);
    do{
        str[0] = num / 1000;
        str[1] = (num - str[0] * 1000) / 100;
        str[2] = (num - str[0] * 1000 - str[1] * 100) / 10;
        str[3] = num % 10;
        if (str[0] == str[1] && str[0] == str[2] && str[0] == str[3])
        {
            printf("%4d - %4d = %04d", num, num, 0);
            break;
        }
        for (count = 0; count < 4; count ++)
        {
            for (time = 0; time < 3 - count;time ++)
            {
                if (str[time] >= str[time+1])
                {
                    int temp = 0;
                    temp = str[time];
                    str[time] = str[time + 1];
                    str[time + 1] = temp;
                }
            }
        }
        max = (str[3] * 1000 + str[2] * 100 + str[1] * 10 + str[0]);
        min = (str[0] * 1000 + str[1] * 100 + str[2] * 10 + str[3]);
        num = max - min;
        printf("%04d - %04d = %04dn", max, min, num);
    }while (num != 6174);
    return 0;

}
/*总结:
由于用一个数组直接分别去存储用户输入的四位数,不能满足循环后仍然可以这么使用,所以再循环里用用数组分别存储数值。
然后就是将一个数组中的元素合并成一个数值,可以考虑通过乘10的一定倍数后相加
再者,在子函数中改变数组的值,相应的子函数的形参也要写成相应的格式

practice makes perfect!

加油

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/291276.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号