当前位置:网站首页>1019 数字黑洞
1019 数字黑洞
2022-06-29 04:09:00 【乐乐~LL】
给定任一个各位数字不完全相同的 4 位正整数,如果我们先把 4 个数字按非递增排序,再按非递减排序,然后用第 1 个数字减第 2 个数字,将得到一个新的数字。一直重复这样做,我们很快会停在有“数字黑洞”之称的 6174,这个神奇的数字也叫 Kaprekar 常数。
例如,我们从6767开始,将得到
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
7641 - 1467 = 6174
... ...
现给定任意 4 位正整数,请编写程序演示到达黑洞的过程。
输入格式:
输入给出一个 (0,104) 区间内的正整数 N。
输出格式:
如果 N 的 4 位数字全相等,则在一行内输出 N - N = 0000;否则将计算的每一步在一行内输出,直到 6174 作为差出现,输出格式见样例。注意每个数字按 4 位数格式输出。
输入样例 1:
6767
输出样例 1:
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
输入样例 2:
2222
输出样例 2:
2222 - 2222 = 0000代码如下:
#include<stdio.h>
int shen(int ar[5])
{
int i, j, k;
for (i = 0; i < 4 - 1; i++)
{
for (j = 0; j < 4 - i - 1; j++)
{
if (ar[j] < ar[j + 1])
{
k = ar[j];
ar[j] = ar[j + 1];
ar[j + 1] = k;
}//降序排列
}
}
int zh = 0;
for (i = 0; i < 4; i++)
zh = zh * 10 + ar[i];
return zh;
}
int jian(int ar[5])
{
int i, j, k;
for (i = 0; i < 4 - 1; i++)
{
for (j = 0; j < 4 - i - 1; j++)
{
if (ar[j] > ar[j + 1])
{
k = ar[j];
ar[j] = ar[j + 1];
ar[j + 1] = k;
}
}
}
int zh = 0;
for (i = 0; i < 4; i++)
zh = zh * 10 + ar[i];
return zh;
}
int fack(int ar[5])
{
int i;
for (i = 0; i < 4 - 1; i++)
if (ar[i] != ar[i + 1])
return 0;
return 1;
}
int pan(int n)
{
if (n >= 1000)
return 0;
else if (n >= 100 && n < 1000)
return 1;
else if (n > 10 && n < 100)
return 2;
else
return 3;
}
void print(int shi,int ji, int f2)
{
printf("%d - ", shi);
switch (f2)
{
case 0:printf("%d", ji); break;
case 1:printf("0%d", ji); break;
case 2:printf("00%d", ji); break;
case 3:printf("000%d", ji); break;
}
printf(" = ");
int f = pan(shi - ji);
switch (f)
{
case 0:printf("%d", shi-ji); break;
case 1:printf("0%d", shi-ji); break;
case 2:printf("00%d", shi-ji); break;
case 3:printf("000%d", shi-ji); break;
}
printf("\n");
}
int main()
{
int a, i, j;
int sh, ji,f2;
int ar[5] = { 0 };
scanf("%d", &a);
//zh = a;
for (i = 0; i < 4; i++,a/=10)
ar[i] = a % 10;
sh=shen(ar);
ji = jian(ar);
j = fack(ar);
// printf("%d - %d = %d\n", sh - ji);
if (j == 1)
//printf("%d - %d = %d\n", sh,ji,sh - ji);
{
f2 = pan(ji);
print(sh, ji, f2);
}
else
{
while (sh - ji != 6174 && j != 1)
{
// int f1 = pan(sh);
f2 = pan(ji);
//printf("%d - %d = %d\n",sh,ji, sh - ji);
print(sh, ji, f2);
a = sh - ji;
for (i = 0; i < 4; i++, a /= 10)
ar[i] = a % 10;
sh = shen(ar);
ji = jian(ar);
j = fack(ar);
}
//printf("%d - %d = %d\n", sh, ji, sh - ji);
//int f3 = pan(sh - ji);
f2 = pan(ji);
print(sh, ji, f2);
}
}
边栏推荐
- My creation anniversary
- Webassembly learning - dynamic linking
- String differences between different creation methods
- IDEA修改jvm内存
- Qtableview gets all currently selected cells
- Yangzhou needs one English IT Helpdesk Engineer -20220216
- Data statistical analysis (SPSS) [3]
- CDC2.2.1还不支持postgresql14.1么?基于pgbouncer连接方式下,以5433
- 【C语言】解决 “address of stack memory associated with local variable ‘num‘ returned”
- logstash启动过慢甚至卡死
猜你喜欢

科技雲報道:混合辦公的B面:安全與效率如何兼得?
![[filter design] customize the filter with MATLAB according to the design index](/img/b2/b5576c5eb6dbe9a0c64158802aa1d0.png)
[filter design] customize the filter with MATLAB according to the design index

在命令行登录mysql数据库以及查看版本号

你为什么做测试/开发程序员?还能回想出来吗......

Cloud native weekly | grafana 9 was officially released; The Chinese version of cloud native vocabulary is now online

If I hadn't talked to Ali P7, I wouldn't know I was a mallet

《运营之光3.0》全新上市——跨越时代,自我颠覆的诚意之作
![[new function] ambire wallet integrates Metis network](/img/29/8a8c0cd40c51cef1174ee59706d4c9.png)
[new function] ambire wallet integrates Metis network

基于xlsx的B+树索引实现

【FPGA+sin】基于DDS(直接数字合成)的正弦信号发生器模块FPGA实现
随机推荐
Error accessing database
Binary tree serialization and deserialization (leetcode (difficult))
MySQL review materials (additional) case when
leetcode - 295. Median data flow
智能视觉组比赛流程草案
【布里渊现象】光纤布里渊温度和应变分布同时测量系统研究
The great gods take connections from the MySQL connection pool in the open of the rich function. The initialization of the connection pool is 20. If the parallelism of the rich function is 1
我的创作纪念日
Qtableview gets all currently selected cells
webassembly学习-动态链接
HCIE-Security Day41:理论学习:信息收集与网络探测
PostgreSQL 出现cross-database references are not implemented的bug
Seattention channel attention mechanism
多机局域网办公神器 rustdesk 使用强推!!!
Call snapstateon closed sou from Oracle CDC
【FPGA+sin】基于DDS(直接数字合成)的正弦信号发生器模块FPGA实现
Project development culture
赚钱的5个层次,你在哪一层?
I came from a major, so I didn't want to outsource
选对学校,专科也能进华为~早知道就好了