当前位置:网站首页>PAT 乙等 1019 C语言
PAT 乙等 1019 C语言
2022-06-23 04:11:00 【章鱼bro】
1019. 数字黑洞 (20)
给定任一个各位数字不完全相同的4位正整数,如果我们先把4个数字按非递增排序,再按非递减排序,然后用第1个数字减第2个数字,将得到一个新的数字。一直重复这样做,我们很快会停在有“数字黑洞”之称的6174,这个神奇的数字也叫Kaprekar常数。
例如,我们从6767开始,将得到
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
7641 - 1467 = 6174
... ...
现给定任意4位正整数,请编写程序演示到达黑洞的过程。
输入格式:
输入给出一个(0, 10000)区间内的正整数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
思路:本题思路将输入的一个四位数打散,存入一个长度为四的数组,分别递增递减排序,再用递减排序的数减去递增排序的数,对结果进行判断,若不是6174,则将差作为参数进行递归调用
一、起始变量
1.N,接受四位正整数
二、运算、
1.将四位数打散存入数组num
2.递增排序
3.递减排序
4.输入相减过程及结果,若为四位一样的数或者相减结果为6174则进行输出,否则将相减结果作为参数进行递归调用
三、代码
#include "stdio.h"
#include "stdlib.h"
void Insperate(int a, int * num);//将输入的数分成四个数存入数组
int Increase(int a);//将四个数递增排序
int Decrease(int a);//将四个数递减排序
void PrintRes(int a);//按照题目要求标准输出结果
int cmp1(const void * a, const void * b);//qsort递增排序
int cmp2(const void * a, const void * b);//qsort递减排序
int main()
{
int N;
scanf("%d",&N);
PrintRes(N);
return 0;
}
int cmp1(const void * a, const void * b)//qsort递增排序
{
return *(int *)a - *(int *)b;
}
int cmp2(const void * a, const void * b)//qsort递减排序
{
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);//使用快排递增排序
ret = num[0] * 1000 + num[1] * 100 + num[2] * 10 +num[3];//合成一个四位数
return ret;
}
int Decrease(int a)
{
int ret;
int num[4];
Insperate(a, num);
qsort(num,4,sizeof(num[0]),cmp2);//使用快排递减排序
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)//递归出口一
{
printf("%4d - %4d = 0000", dec, inc);//如果为四个一样的数,直接输出
return;
}
else if(sub == 6174)//递归出口二
{
printf("%04d - %04d = %04d",dec,inc,sub);
return;
}
else
{
printf("%04d - %04d = %04d",dec,inc,sub);
printf("\n");
PrintRes(sub);//将结果进行递推调用
}
}边栏推荐
- TCP/IP 详解(第 2 版) 笔记 / 3 链路层 / 3.4 网桥与交换机
- Software project management 8.4 Software project quality plan
- Yingjixin launched 4 series of lithium batteries 100W mobile power supply voltage rise and fall scheme SOC chip ip5389
- Build a gocd environment
- 英集芯IP5566带TYPE-C口3A充放快充移动电源5w无线充二合一方案SOC
- PAT 乙等 1011 C语言
- CF [1700d] D. River locks (DP, bisection, Mathematics)
- MySQL Foundation
- ORB_ Slam2 operation
- 阿里云对象存储oss+picgo+typora实现构建图床
猜你喜欢
![[image fusion] sparse regularization based on non convex penalty to realize image fusion with matlab code](/img/e2/24eb2a60e3dc603b3ec4bfefd0b8e5.png)
[image fusion] sparse regularization based on non convex penalty to realize image fusion with matlab code

sprintf 格式代码使用不规范在不同平台下的表现

Differences between fs4059a and fs5080e charging chips

Jvm: when a method is overloaded, the specific method to call is determined by the static type of the incoming parameter rather than the actual type of the parameter

Yingjixin ip6806 wireless charging scheme 5W Qi certified peripheral simplified 14 devices

MDM data cleaning function development description

STC 32位8051单片机开发实例教程 一 开发环境搭建

51万奖池邀你参战——第二届阿里云ECS CloudBuild开发者大赛来袭

Wechat applet: unfashionable love talk

Redis缓存穿透解决方案-布隆过滤器
随机推荐
MySQL面试真题(二十五)——常见的分组比较场景
英集芯ip6806无线充电方案5W过Qi认证外围精简14颗器件
MDM数据清洗功能开发说明
Lihongyi, machine learning 5 Tips for neural network design
True question of MySQL interview (29) -- case - finding favorite movies
Export PDF with watermark
ORB_SLAM2运行
Ansible 使用普通用户管理被控端
AHA C language Chapter 7 you can do more with it (talks 27-28)
Special research on Intelligent upgrading of heavy trucks in China in 2022
Software project management 8.4 Software project quality plan
Shifu, the open source development platform of the Internet of things, is open for internal testing! Release of the first version of technical documents
英文字母pc是什么意思,互联网的pc指的是什么
雷达图canvas
1010 Radix
104. simple chat room 7: use socket to transfer objects
Fs2119a Synchronous Boost IC output 3.3V and fs2119b Synchronous Boost IC output 5V
STC 32位8051单片机开发实例教程 一 开发环境搭建
MDM data cleaning function development description
Low cost 5W wireless charger scheme fs68001b simple charging chip