当前位置:网站首页>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);
}
}
边栏推荐
- Build a simple website by yourself
- Yangzhou needs one English IT Helpdesk Engineer -20220216
- Data collection and management [11]
- moudo网络库剖析
- 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
- 泠静的想一想自己的路
- Ask the handler about the memory leak scenario in the interview. Don't just know static internal classes & weak references!
- Logstash starts too slowly or even gets stuck
- How to back up all data on Apple mobile phone in 2 steps (free)
- Implementation of b+ tree index based on xlsx
猜你喜欢

不使用union实现Mysql 列转行

Anaconda自带的Spyder编辑器启动报错问题

Idea modifying JVM memory

yolox出现 RuntimeError: DataLoader worker (pid(s) 17724, 1364, 18928) exited unexpectedly

HCIE-Security Day41:理论学习:信息收集与网络探测

c语言 --- 分支结构

MySQL column to row conversion without Union

Ask the handler about the memory leak scenario in the interview. Don't just know static internal classes & weak references!

Whose encryption key is written in the code? (that's you)

New listing of operation light 3.0 -- a sincere work of self subversion across the times
随机推荐
泠静的想一想自己的路
自己动手搭建一个简单的网站
大神们 在富函数的open中从mysql连接池里取连接 连接池初始化是20个 如果富函数的并行度是1
项目开发修养
The second meeting of the Second Council of Euler open source community was held, and Xinhua III, hyperfusion and Godson Zhongke became members of the Council
Data collection and management [3]
Mécanisme d'attention du canal de fixation
Emotional changes need to be controlled
Build a simple website by yourself
Anaconda's own Spyder editor starts with an error
Wi-Fi 7 来啦,它到底有多强?
Data collection and management [12]
科技云报道:混合办公的B面:安全与效率如何兼得?
科班出身,结果外包都不要
5-minute NLP: summary of time chronology from bag of words to transformer
Analysis of moudo Network Library
Data collection and management [13]
【新功能】Ambire 钱包集成了 Metis 网络
lua-protobuff emmy-lua 轮子
MySQL review materials (additional) case when