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

问题描述:
所谓卡布列克运算是指对任意一个四位数,只要它们各个位上的数字不完全相同,就是这样的规律:
(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)

实例三十九:卡布列克运算相关推荐

  1. java卡布列克运算_求验证卡布列克运算的代码及详解

    网络搜索的(Pascal版.C版.C 版):看了一下,根据卡布列克常数的定义,下面的几种程序实现包括输入数.数的各位取出.排序以求最大和最小.求差最后获取该常数. 卡布列克常数 验证卡布列克运算. 任 ...

  2. C语言基础题OJ 验证卡布列克运算

    文章目录 题目描述 题目背景 输入输出提示 程序运行实例 一.解题思路 二.题解 源代码 写在最后 题目描述 题目背景 美国有位数学家叫卡布列克,他整日埋头在数学计算中.一天,他忽然发现一个有趣的数学 ...

  3. c语言 验证卡布列克运算,pasca编程验证卡布列克运算

    公告: 为响应国家净网行动,部分内容已经删除,感谢读者理解. 话题:pasca编程验证卡布列克运算 问题详情:验证卡布列克运算,对给定的四位数的各位数字重新排序,构成一回答:var a:array[1 ...

  4. C语言编程练习 6.验证卡布列克运算。即:任意一个四位数,只要它们各个位上的数字是不全相同的,就有这样的规律

    题目描述: *验证卡布列克运算.即:任意一个四位数,只要它们各个位上的数字是不全相同的,就有这样的规律: (1)将组成该四位数的四个数字由大到小排列,形成由这四个数字构成的最大的四位数: (2)将组成 ...

  5. 【C语言】验证卡布列克运算。

    #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> # ...

  6. C/C++卡布列克运算验证

    #include<iostream>using namespace std;int main(void){int sortmax(int);int sortmin(int);int i;i ...

  7. C语言编程验证卡布列克,C趣味程序百例(26)卡布列克常数

    83.卡布列克常数 验证卡布列克运算.任意一个四位数,只要它们各个位上的数字是不全相同的,就有这样的规律: 1)将组成该四位数的四个数字由大到小排列,形成由这四个数字构成的的四位数: 2)将组成该四位 ...

  8. 100个python算法超详细讲解:卡布列克常数

    [100个python算法超详细讲解]@谷哥技术 1.问题描述 对于任意一个4位数n,进行如下的运算: 1)将组成该4位数的4个数字由大到小排列,形成由这4个数字构 成的最大的4位数. 2)将组成该4 ...

  9. C# 实现卡布列克数

    卡布列克是指任意一个四位数,只要他们各个位上的数字不相同,有这样的规律. 一.把组成这个四位数的各个数字从大到小排列,组成一个最大的四位数. 二.把组成这个四位数的各个数字从小到大排列,组成一个最小的 ...

最新文章

  1. C#创建TCP/IP服务端和客户端,含测试demo及源码
  2. 安卓怎么下载python-教你在安卓手机上安装python程序
  3. SH 脚本注意事项之 IF 判断
  4. 四十六、MongoDB数据库学习
  5. c#操作mysql数据库
  6. 面试常考题:不调用库函数,怎样实现字符串操作函数?
  7. protobuf3 自定义option_ProtoBuf3语法指南(Protocol Buffers)_下
  8. 【SaaS】企业微信裂变系统引流变现系统产品介绍
  9. java 反解析cron_Java解析Cron表达式
  10. C语言课设家庭财务小管家(大作业)
  11. 怎么配置内网IP SSL证书?
  12. 解决Chrome浏览器变慢
  13. 【论文翻译】Many-Class Few-Shot Learning on Multi-Granularity Class Hierarchy
  14. 【从饮水机到名人堂之c语言】操作符详解(1)
  15. Java基础之匿名内部类,匿名内部类是什么?为什么要用匿名内部类,匿名内部类详解。
  16. QQ 简洁模式切换失败解决方法
  17. java user.dir 设置_关于user.dir的认识
  18. 让旧衣服换新颜 听听章泽天怎么说!
  19. windows桌面怎么添加计算机,Windows桌面添加我的电脑
  20. 开发amis工作日历组件

热门文章

  1. OSChina 周三乱弹 —— 女友站在女友和闺蜜合影的照片前
  2. photoshop切片使用教程
  3. 关于企业中网络安全建设的一些心得
  4. AngularJS是什么?
  5. 2013年7月16日 18:28:32 GPS应用
  6. 电脑开机慢的解决办法
  7. 5.6日华为笔试第三题解法
  8. php 录小视频,这些短视频拍摄小技巧,99%的人不知道
  9. span class=red[置顶]/span分组对称加密模式:ECB/CBC/CFB/OFB缺CTR- -
  10. 摄像头录像 及视频保存压缩