当前位置:网站首页>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
}
}边栏推荐
- True MySQL interview question (21) - Finance - overdue loan
- MySQL character set
- Visual Studio调试技巧
- MySQL面试真题(二十七)——RFM分析法对用户进行分类
- MDM data cleaning function development description
- JS面试题----防抖函数
- jvm-01. Instruction rearrangement
- Alibaba cloud object storage oss+picgo+typera implements the construction map
- jvm-05.垃圾回收
- Huawei's software and hardware ecosystem has taken shape, fundamentally changing the leading position of the United States in the software and hardware system
猜你喜欢

Kotlin android简单Activity跳转、handler和thread简单配合使用

How can digital collections empower economic entities?

数字藏品火热背后需要强大的技术团队支持 北方技术团队

Software design and Development Notes 2: serial port debugging tool based on QT design

新课上线 | 每次 5 分钟,轻松玩转阿里云容器服务!

数字藏品市场才刚刚开始

New classes are launched | 5 minutes each time, you can easily play with Alibaba cloud container service!

Heimdall database proxy scale out 20 times

MySQL面试真题(二十五)——常见的分组比较场景

A bit of knowledge - folding forging and Damascus steel
随机推荐
MySQL面试真题(二十八)——案例-通讯运营商指标分析
MySQL面试真题(二十二)——表连接后的条件筛选及分组筛选
Dolphin scheduler dolphin scheduling upgrade code transformation -upgradedolphin scheduler
Advanced Mathematics (Seventh Edition) Tongji University exercises 1-9 personal solutions
PAT 乙等 1014 C语言
Real MySQL interview question (XXVIII) -- case - Analysis of indicators of communication operators
Pat class B 1010 C language
App SHA1 acquisition program Baidu map Gaode map simple program for acquiring SHA1 value
TCP/IP 详解(第 2 版) 笔记 / 3 链路层 / 3.3 全双工, 节能, 自动协商机制, 802.1X 流控制 / 3.3.3 链路层流量控制
Kotlin android简单Activity跳转、handler和thread简单配合使用
Real MySQL interview question (30) -- shell real estate order analysis
Android handler memory leak kotlin memory leak handling
node中操作mongoDB
Oracle exception
AHA C language Chapter 7 you can do more with it (talks 27-28)
jvm-04. Object's memory layout
华为软硬件生态圈成型,从根子上改变美国对软硬件体系的领导地位
Real MySQL interview questions (XXVII) -- Classification of users by RFM analysis method
android Handler内存泄露 kotlin内存泄露处理
What is the magic of digital collections? Which reliable teams are currently developing