当前位置:网站首页>1019 digital black hole
1019 digital black hole
2022-06-29 04:11:00 【Lele ~ll】
Given any number that is not exactly the same 4 Positive integer , If we put 4 Numbers are sorted in non incremental order , And then in non decreasing order , Then use the 1 A number minus 2 A digital , Will get a new number . Keep doing this again and again , We will soon stop having “ Digital black hole ” It's called. 6174, This magic number is also called Kaprekar constant .
for example , We from 6767 Start , Will get
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
7641 - 1467 = 6174
... ...
Any 4 Positive integer , Please write a program to demonstrate the process of reaching the black hole .
Input format :
Type to give a (0,104) A positive integer in an interval N.
Output format :
If N Of 4 The digits are all equal , Then output... In one line N - N = 0000; Otherwise, each step of the calculation will be output in one line , until 6174 Appear as a difference , See the example for the output format . Pay attention to each number press 4 Bit format output .
sample input 1:
6767
sample output 1:
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
sample input 2:
2222
sample output 2:
2222 - 2222 = 0000The code is as follows :
#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;
}// Descending order
}
}
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);
}
}
边栏推荐
- SEAttention 通道注意力機制
- Data collection and management [12]
- NotImplementedError: Could not run torchvision::nms
- mysql动态加表只能加小表,加大表跑着跑着任务就不读数据了,有什么解决办法吗
- Data collection and management [11]
- 【新功能】Ambire 钱包集成了 Metis 网络
- Blue Bridge Cup ruler method
- Yangzhou needs one English IT Helpdesk Engineer -20220216
- Logstash starts too slowly or even gets stuck
- Path and LD_ LIBRARY_ Example of path usage
猜你喜欢

为什么说测试岗位是巨坑?8年测试人告诉你千万别上当

SEAttention 通道注意力機制

Libuv library overview and comparison of libevent, libev and libuv (Reprint)

Idea modifying JVM memory

欧拉开源社区第二届理事会第二次会议召开,新华三、超聚变和龙芯中科成为理事会成员单位

云原生周报 | Grafana 9正式发布;云原生词汇表中文版现已上线

科班出身,结果外包都不要
![[Brillouin phenomenon] Study on simultaneous measurement system of Brillouin temperature and strain distribution in optical fiber](/img/92/57792ef733964230d36e0b9bb218b4.png)
[Brillouin phenomenon] Study on simultaneous measurement system of Brillouin temperature and strain distribution in optical fiber
![[new function] ambire wallet integrates Metis network](/img/29/8a8c0cd40c51cef1174ee59706d4c9.png)
[new function] ambire wallet integrates Metis network

If I hadn't talked to Ali P7, I wouldn't know I was a mallet
随机推荐
If you choose the right school, you can enter Huawei as a junior college. I wish I had known
Data collection and management [10]
要不是和阿里P7聊过,我也不知道自己是个棒槌
Project development culture
Technology cloud report: side B of mixed office: how to have both security and efficiency?
Binary tree serialization and deserialization (leetcode (difficult))
C language -- branch structure
科班出身,结果外包都不要
Path and LD_ LIBRARY_ Example of path usage
Data collection and management [15]
String不同创建方式的区别
大神们 在富函数的open中从mysql连接池里取连接 连接池初始化是20个 如果富函数的并行度是1
Error accessing database
热更新流程
Does cdc2.2.1 not support postgresql14.1? Based on the pgbouncer connection mode, with 5433
Web crawler knowledge day04
If I hadn't talked to Ali P7, I wouldn't know I was a mallet
[C language] explain the thread exit function pthread_ exit
【C语言】 详解线程退出函数 pthread_exit
Canoe- how to parse messages and display information in the trace window (use of program node and structure type system variables)