当前位置:网站首页>Pat class B 1019 C language
Pat class B 1019 C language
2022-06-23 05:57:00 【Octopus bro】
1019. Digital black hole (20)
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, 10000) 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:6767sample output 1:
7766 - 6677 = 1089 9810 - 0189 = 9621 9621 - 1269 = 8352 8532 - 2358 = 6174sample input 2:
2222sample output 2:
2222 - 2222 = 0000
Ideas : The idea of this question is to break up a four digit input , Store an array of four in length , Sort in ascending and descending order respectively , Then subtract the number of ascending sort from the number of descending sort , Judge the result , If it is not 6174, Recursively call the difference as a parameter
One 、 Starting variable
1.N, Accept a four digit positive integer
Two 、 operation 、
1. Break up the four digits and store them in the array num
2. Incremental sort
3. Descending sort
4. Input subtraction process and result , If it is a number with the same four digits or the result of subtraction is 6174 Then output , Otherwise, the subtraction result is called recursively as a parameter
3、 ... and 、 Code
#include "stdio.h"
#include "stdlib.h"
void Insperate(int a, int * num);// Divide the input number into four numbers and store them in the array
int Increase(int a);// Sort the four numbers incrementally
int Decrease(int a);// Sort the four numbers in descending order
void PrintRes(int a);// Output the results according to the requirements of the topic
int cmp1(const void * a, const void * b);//qsort Incremental sort
int cmp2(const void * a, const void * b);//qsort Descending sort
int main()
{
int N;
scanf("%d",&N);
PrintRes(N);
return 0;
}
int cmp1(const void * a, const void * b)//qsort Incremental sort
{
return *(int *)a - *(int *)b;
}
int cmp2(const void * a, const void * b)//qsort Descending sort
{
return *(int *)b - *(int *)a;
}
void Insperate(int a,int * num)
{
int i = 0;
for(i = 0; i < 4; i++)
{
num[i] = a % 10;
a /= 10;
}
}
int Increase(int a)
{
int ret;
int num[4];
Insperate(a, num);
qsort(num,4,sizeof(num[0]),cmp1);// Use quick sort incremental sort
ret = num[0] * 1000 + num[1] * 100 + num[2] * 10 +num[3];// Compose a four digit number
return ret;
}
int Decrease(int a)
{
int ret;
int num[4];
Insperate(a, num);
qsort(num,4,sizeof(num[0]),cmp2);// Use fast descending sort
ret = num[0] * 1000 + num[1] * 100 + num[2] * 10 +num[3];
return ret;
}
void PrintRes(int a)
{
int inc,dec;
inc = Increase(a);
dec = Decrease(a);
int sub = dec - inc;
if(sub == 0)// Recursive exit one
{
printf("%4d - %4d = 0000", dec, inc);// If it's four identical numbers , Direct output
return;
}
else if(sub == 6174)// Recursive exit 2
{
printf("%04d - %04d = %04d",dec,inc,sub);
return;
}
else
{
printf("%04d - %04d = %04d",dec,inc,sub);
printf("\n");
PrintRes(sub);// Make a recursive call to the result
}
}边栏推荐
- 【斯坦福计网CS144项目】Lab2: TCPReceiver
- 数字藏品赋能实体产业释放了哪些利好?
- Adnroid activity截屏 保存显示到相册 View显示图片 动画消失
- Use of visdom
- Basic calculator for leetcode topic analysis
- JS interview question - anti shake function
- MDM data cleaning function development description
- PAT 乙等 1020.月饼
- The hierarchyviewer tool cannot find the hierarchyviewer location
- Work accumulation - judge whether GPS is on
猜你喜欢

jvm-06. Garbage collector
![[proteus simulation] Arduino uno+pcf8574+lcd1602+mpx4250 electronic scale](/img/2d/96a370c90dcb7091038afad33bc4b4.png)
[proteus simulation] Arduino uno+pcf8574+lcd1602+mpx4250 electronic scale
![[Stanford Jiwang cs144 project] lab2: tcpreceiver](/img/70/ceeca89e144907226f29575def0e4d.png)
[Stanford Jiwang cs144 project] lab2: tcpreceiver

The construction of digital factory can be divided into three aspects

C primer plus learning notes - 2. Constant and formatted IO (input / output)

云原生数据库是未来

【开源项目】excel导出lua配置表工具

jvm-03.jvm内存模型

Analysis on the problems and causes of digital transformation of manufacturing industry

HierarchyViewer工具找不到 HierarchyViewer位置
随机推荐
Redis cache penetration solution - bloom filter
MySQL character set
[Stanford Jiwang cs144 project] lab2: tcpreceiver
Genetic engineering of AI art? Use # artbreeder to change any shape of the image
Work accumulation - judge whether GPS is on
Wireshark TS | video app cannot play
The hierarchyviewer tool cannot find the hierarchyviewer location
PAT 乙等 1022 D进制的A+B
iNFTnews | 加密之家从宇宙寄来的明信片,你会收到哪一张?
PAT 乙等 1014 C语言
True MySQL interview question (21) - Finance - overdue loan
jvm-03.jvm内存模型
C primer plus学习笔记 —— 2、常量与格式化IO(输入/输出)
Excel sheet column title for leetcode Title Resolution
JS面试题----防抖函数
The 510000 prize pool invites you to participate in the competition -- the second Alibaba cloud ECS cloudbuild developer competition is coming
True question of MySQL interview (29) -- case - finding favorite movies
Real MySQL interview questions (25) -- common group comparison scenarios
Basic calculator II for leetcode topic analysis
Wechat applet: a new interesting test