当前位置:网站首页>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
}
}边栏推荐
- [graduation season u; advanced technology Er] farewell to the confused self in the past two years. Regroup, junior I'm coming
- 雷达图canvas
- Explicability of counter attack based on optimal transmission theory
- C primer plus學習筆記 —— 2、常量與格式化IO(輸入/輸出)
- Three most advanced certifications, two innovative technologies and two outstanding cases, Alibaba cloud appeared at the cloud native industry conference
- Leetcode topic analysis: factorial training zeroes
- Wireshark TS | 视频 APP 无法播放问题
- 如何指定pig-register项目日志的输出路径
- HierarchyViewer工具找不到 HierarchyViewer位置
- The hierarchyviewer tool cannot find the hierarchyviewer location
猜你喜欢

Opportunities and challenges of digital collections from the perspective of technology development team

三项最高级认证,两项创新技术、两大优秀案例,阿里云亮相云原生产业大会

Use of visdom

True MySQL interview question (21) - Finance - overdue loan

Heimdall database proxy scale out 20 times

ssm项目搭建

How can digital collections empower economic entities?

The digital collection market has just begun

True MySQL interview question (24) -- row column exchange

Explicability of counter attack based on optimal transmission theory
随机推荐
Basic calculator for leetcode topic analysis
Real MySQL interview question (23) -- pinduoduo ball game analysis
【owt】owt-client-native-p2p-e2e-test vs2017构建 6:修改脚本自动生成vs工程
MySQL面试真题(二十四)——行列互换
C primer plus學習筆記 —— 2、常量與格式化IO(輸入/輸出)
AHA C language Chapter 7 you can do more with it (talks 27-28)
The traditional Internet like platform may no longer exist, and a new industry integrating industrial characteristics and Internet characteristics
jvm-02. Guarantee of orderliness
Visual Studio调试技巧
Explicability of counter attack based on optimal transmission theory
TCP/IP 详解(第 2 版) 笔记 / 3 链路层 / 3.4 网桥与交换机
Visdom draws multiple dynamic loss curves
Alibaba cloud object storage oss+picgo+typera implements the construction map
Centos7 deploy radius service -freeradius-3.0.13-15 EL7 integrating MySQL
Opencv display image
Genetic engineering of AI art? Use # artbreeder to change any shape of the image
Activity startup mode and life cycle measurement results
MySQL面试真题(二十六)——滴滴2020年笔试题
Ansible 使用普通用户管理被控端
jvm-04. Object's memory layout