实例三十九:卡布列克运算

实例三十九:卡布列克运算

问题描述:
所谓卡布列克运算是指对任意一个四位数,只要它们各个位上的数字不完全相同,就是这样的规律:
(1)把组成这个四位数的四个数字重新生成最大的四位数;
(2)把组成这个四位数的四个数字重新生成最小的四位数(若四个数字中含有0,则此四位数可小于四位);
(3)求出以上两数之差,得到一个新的四位数。
重复以上过程,总能得到最后的结果为6174。
试编写卡布列克运算的验证程序。
The so-called Kabrek operation refers to any four digits, as long as the numbers on their respective bits are not exactly the same, this is the law:
(1) Regenerate the four digits that make up the four digits to the maximum four digits;
(2) Regenerate the smallest four digits of the four digits that make up the four digits (if the four digits contain 0, the four digits can be less than four digits);
(3) Find the difference between the above two numbers and get a new four-digit number.
Repeat the above process and always get the final result of 6174.
Try writing the verification program for the Kabrek operation.

算法思路:

分三个模块解决:
(1)将一个四位数的每一位数字按从大到小(或从小到大)的顺序放到一个一维数组中;
(2)将(1)中有序数组从左到右(或从右到左)组成一个最大的四位数;
(3)将(1)中有序数组从右到左(或从左到右)组成一个最小的四位数;
It is solved by three modules:
(1) Put each four-digit number in a one-dimensional array from large to small (or small to large);
(2) Forming an ordered array in (1) from left to right (or from right to left) to form a maximum four digits;
(3) Forming an ordered array in (1) from right to left (or from left to right) to form a minimum four-digit number;

/*实例三十九:卡布列克运算*/

#include<stdio.h>

int idigit(int n,int a[4])                 //取得四位数,并按照从大到小的顺序排列
{
    int i=0,j,t;
    for(i=0;i<4;i++)
    {
        a[i] = n % 10;
        n = n / 10;
    }
    for(i=0;i<3;i++)                        //比较四个数(比三次),并从大到小依次存放
        for(j=i+1;j<4;j++)
            if(a[i]<a[j])
            {
                t=a[i];
                a[i]=a[j];
                a[j]=t;
            }
    return 1;
}

int getmin(int n)                           //重新生成最小的四位数m
{
    int a[4],i,j,t,m=0;                     //数组;两个循环变量;各位置值t;传递继承值m;
    idigit(n,a);
    for(i=3;i>=0;i--)                       //数组的最小情况(倒着)
    {
        t = 1;
        for(j=0;j<i;j++)                    //循环3+2+1+0=6次
            t *= 10;
        m += t * a[i];
    }
    return m;
}

int getmax(int n)                           //重新生成最大的四位数m
{
    int a[4],i,j,t,m=0;
    idigit(n,a);
    for(i=0;i<4;i++)
    {
        t = 1;
        for(j=0;j<3-i;j++)
            t *= 10;
        m += t * a[i];
    }
    return m;
}

int main()
{
    int n;
    printf("Please enter a four digit number that is not the same as each digit:\n");
    scanf("%d",&n);
    while(n!=6174)
    {
        printf("%4d - %4d = %4d\n",getmax(n),getmin(n),getmax(n)-getmin(n));
        n = getmax(n) - getmin(n);
    }
    return 0;
}

程序心得:

本次运用到四位数的分离排序和求最小数和最大数的方法,我们将它们设计成为不同的独立模块。
This time we applied four-digit separation ordering and the method of finding the minimum and maximum numbers, we designed them into different independent modules.

拓展思考:

1.实际上可以直接将求最大最小的算法写在一块(主要解决返回两个值的问题,考虑用数组和指针处理解决)。
1. In fact, you can directly write the algorithm that seeks the largest and the smallest (mainly solve the problem of returning two values, consider solving with array and pointer processing).
2.能否将程序段Can you change block

for(i=0;i<4;i++)
    {
        a[i] = n % 10;
        n = n / 10;
    }

改为to

do
{
	a[i++] = n % 10;
}while((n/=10)!=0)
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C/C++语言经典实用趣味程序设计编程百例精解(1) <br>(详解收藏在)http://www.klfd.net.cn/?p=384 <br>1.绘制余弦曲线 <br>2.绘制余弦曲线和直线 <br>3.绘制圆 <br>4.歌星大奖赛 <br>5.求最大数 <br>6.高次方数的尾数 <br>7.阶乘尾数零的个数 <br>8.借书方案知多少 <br>9.杨辉三角形 <br>10.数制转换 <br><br>C/C++语言经典实用趣味程序设计编程百例精解(2) <br>(详解收藏在)http://www.klfd.net.cn/?p=385 <br>11.打鱼还是晒网 <br>12.抓交通肇事犯 <br>13.该存多少钱 <br>14.怎样存钱利最大 <br>15.捕鱼和分鱼 <br>16.出售金鱼 <br>17.平分七筐鱼 <br>18.有限5位数 <br>19.8除不尽的自然数 <br>20.一个奇异的三位数 <br><br> C/C++语言经典实用趣味程序设计编程百例精解(3) <br>(详解收藏在)http://www.klfd.net.cn/?p=386 <br>21.4位反序数 <br>22.求车速 <br>23.由两个平方三位数获得三个平方二位数 <br>24.阿姆斯特朗数 <br>25.完全数 <br>26.亲密数 <br>27.自守数 <br>28.回文数 <br>29.求具有abcd=(ab+cd)2性质的四位数 <br>30.求素数 <br><br>C/C++语言经典实用趣味程序设计编程百例精解(4) <br>(详解收藏在)http://www.klfd.net.cn/?p=387 <br>31.歌德巴赫猜想 <br>32.可逆素数 <br>33.回文素数 <br>34.要发就发 <br>35.素数幻方 <br>36.百钱百鸡问题 <br>37.爱因斯坦的数学题 <br>38.换分币 <br>39.年龄几何 <br>40.三色球问题 <br><br> C/C++语言经典实用趣味程序设计编程百例精解(5) <br>(详解收藏在)http://www.klfd.net.cn/?p=388 <br>41.马思手稿中的数学题 <br>42.最大公约数和最小公倍数 <br>43.分数比较 <br>44.分数之和 <br>45.将真分数分解为埃及分数 <br>46.列出真分数序列 <br>47.计算分数的精确值 <br>48.新娘和新郞 <br>49.委派任务 <br>50.谁在说谎 <br><br>C/C++语言经典实用趣味程序设计编程百例精解(6) <br>(详解收藏在)http://www.klfd.net.cn/?p=389 <br>51.谁是窃贼 <br>52.黑与白 <br>53.迷语博士的难题(1) <br>54.迷语博士的难题(2) <br>55.哪个大夫哪天值班 <br>56.区分旅客国籍 <br>57.谁家孩子跑最慢 <br>58.拉丁方阵 <br>59.填表格 <br>60.1~9分成1:2:3的三个3位数 <br><br>C/C++语言经典实用趣味程序设计编程百例精解(7) <br>(详解收藏在)http://www.klfd.net.cn/?p=390 <br>61.1~9组成三个3位的平方数 <br>62.由8个整数形成奇特的立方体 <br>63.减式还原 <br>64.乘式还原 <br>65.乘式还原(2) <br>66.除式还原(1) <br>67.除式还原(2) <br>68.九位累进可除数 <br>69.魔术师的猜牌术(1) <br>70.魔术师的猜牌术(2) <br><br>C/C++语言经典实用趣味程序设计编程百例精解(8) <br>(详解收藏在)http://www.klfd.net.cn/?p=391 <br>71.约瑟夫问题 <br>72.邮票组合 <br>73.和数能表示1~23的5个正整数 <br>74.可称1~40磅的4块砝码 <br>75.10个小孩分糖果 <br>76.小明买书 <br>77.波松瓦酒的分酒趣题 <br>78.求π的近似值 <br>79.求π的近似值(2) <br>80.奇数平方的一个有趣性质 <br><br>C/C++语言经典实用趣味程序设计编程百例精解(9) <br>(详解收藏在)http://www.klfd.net.cn/?p=392 <br>81.角谷猜想 <br>82.四方定理 <br>83.卡布列常数 <br>84.尼科彻斯定理 <br>85.回文数的形成 <br>86.自动发牌 <br>87.黑白子交换 <br>88.常胜将军 <br>89.抢30 <br>90.搬山游戏 <br><br>C/C++语言经典实用趣味程序设计编程百例精解(10) <br>(详解收藏在)http://www.klfd.net.cn/?p=393 <br>91.人机猜数游戏 <br>92.人机猜数游戏(2) <br>93.汉诺塔 <br>94.兎子产子 <br>95.将阿拉伯数字转换为罗马数字 <br>96.选美比赛 <br>97.满足特异条件的数列 <br>98.八皇后问题 <br>99.超长正整数的加法 <br>100.数字移动 <br>
引用\[1\]:这段代码是一个C++程序,用于实现卡布列圆舞曲。它通过对输入的数字进行分解和排序,然后计算最大值和最小值之间的差值,不断迭代直到出现循环节为止。其中使用了一些C++的语法和库函数,比如map、vector和sort等。\[1\] 引用\[2\]:另外一段代码也是用于实现卡布列圆舞曲的,它使用了一个数组来存储每一次迭代的结果,并通过比较判断是否出现了循环节。代码中的input函数用于分解每一个数字,maxn函数用于计算最大值,minn函数用于计算最小值。\[2\] 引用\[3\]:卡布列圆舞曲是一种特殊的循环现象,对于K位数,它们不是变成一个数,而是在几个数字之间形成循环。例如,对于五位数54321,经过一系列计算后会形成循环节82962、75933、63954和61974。\[3\] 问题: 请问这段代码是用来实现什么功能的? 回答: 这段代码是用来实现卡布列圆舞曲的功能。它通过对输入的数字进行分解和排序,然后计算最大值和最小值之间的差值,不断迭代直到出现循环节为止。代码中的两段实现方式略有不同,但都是为了达到同样的目的。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* [P1532 卡布列圆舞曲](https://blog.csdn.net/qq_33942309/article/details/122457948)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [VIJOS-卡布列圆舞曲(模拟)-c++](https://blog.csdn.net/Mr_Shadow_/article/details/77431901)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [vijos卡布列圆舞曲](https://blog.csdn.net/Diva_/article/details/128594588)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值