【PAT】1069. The Black Hole of Numbers (20)

2024-04-12 06:18
文章标签 20 numbers pat black 1069 hole

本文主要是介绍【PAT】1069. The Black Hole of Numbers (20),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目描述

For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in non-increasing order first, and then in non-decreasing order, a new number can be obtained by taking the second number from the first one. Repeat in this manner we will soon end up at the number 6174 – the “black hole” of 4-digit numbers. This number is named Kaprekar Constant.

For example, start from 6767, we’ll get:

7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
7641 - 1467 = 6174
… …

Given any 4-digit number, you are supposed to illustrate the way it gets into the black hole.

翻译:对于除了4位数字一样的任意一个四位数字整数,如果我们降序排列这些数字,再升序排列这些数字,一个新的数字可以通过第一个数减第二个数来得到。重复此过程我们很快就会在数字6174—4位数字的“黑洞”停下。这个数字叫做Kaprekar常数。举个例子,从6767开始,我们将会得到:
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
7641 - 1467 = 6174
……
给你任意一个四位数字,你需要说明它进入黑洞的过程。

INPUT FORMAT

Each input file contains one test case which gives a positive integer N in the range (0, 10000).

翻译:每个输入文件包含一组测试数据,包括一个在(0,10000)范围内的正整数。

OUTPUT FORMAT

If all the 4 digits of N are the same, print in one line the equation “N - N = 0000”. Else print each step of calculation in a line until 6174 comes out as the difference. All the numbers must be printed as 4-digit numbers.

翻译:如果所有4位数字都一样,则输出一行”N - N = 0000”。否则输出每一步计算结果直到它们的差为6174。所有数字必须按照四位数字输出。


Sample Input 1:

6767

Sample Output 1:

7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174


Sample Input 2:

2222

Sample Output 2:

2222 - 2222 = 0000


解题思路

这道题注意一定要做一次循环,否则输入6174可能会没输出,还有每次将数字转换成数组时需要将数组清零。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#define INF 99999999
using namespace std;
int N;
int a[4],b[4];
void transform(int x){int ccount=0;memset(a,0,sizeof(a));while(x){a[ccount++]=x%10;x/=10;}
}
int Num(int x[]){int sum=0;for(int i=3;i>=0;i--){sum=sum*10+x[i];}return sum;
}
int main(){ scanf("%d",&N);transform(N);while(1){sort(a,a+4);for(int i=0;i<4;i++)b[i]=a[3-i];int Max=Num(a);int Min=Num(b);int temp=Max-Min;printf("%04d - %04d = %04d\n",Max,Min,temp);if(temp==0||temp==6174)break;else transform(temp);}return 0;
}

这篇关于【PAT】1069. The Black Hole of Numbers (20)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/896327

相关文章

20.Spring5注解介绍

1.配置组件 Configure Components 注解名称说明@Configuration把一个类作为一个loC容 器 ,它的某个方法头上如果注册7@Bean , 就会作为这个Spring容器中的Bean@ComponentScan在配置类上添加@ComponentScan注解。该注解默认会扫描该类所在的包下所有的配置类,相当于之前的 <context:component-scan>@Sc

PAT-1039 到底买不买(20)(字符串的使用)

题目描述 小红想买些珠子做一串自己喜欢的珠串。卖珠子的摊主有很多串五颜六色的珠串,但是不肯把任何一串拆散了卖。于是小红要你帮忙判断一下,某串珠子里是否包含了全部自己想要的珠子?如果是,那么告诉她有多少多余的珠子;如果不是,那么告诉她缺了多少珠子。为方便起见,我们用[0-9]、[a-z]、[A-Z]范围内的字符来表示颜色。例如,YrR8RrY是小红想做的珠串;那么ppRYYGrrYBR2258可以

PAT-1028

题目描述 某城镇进行人口普查,得到了全体居民的生日。现请你写个程序,找出镇上最年长和最年轻的人。这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过200岁的老人,而今天是2014年9月6日,所以超过200岁的生日和未出生的生日都是不合理的,应该被过滤掉。 输入描述: 输入在第一行给出正整数N,取值在(0, 105];随后N行,每行给出1个人的姓名(由不超过5个

HTML(20)——定位

定位 作用:灵活的改变盒子在网页中的位置 实现: 定位模式:position边偏移:设置盒子的位置 leftrighttopbottom 相对定位 position:relative 改变位置的参照物是自己原来的位置,并且不脱标占位,标签显示模式特点不变 绝对定位 position:absolute 使用场景:子级绝对定位,父级相对定位  脱标不占位参照物:先找最近的已经

C++20中的Feature Test Mocros

C++20定义了一组预处理器宏,用于测试各种语言和库的feature。       Feature Test Mocros(特性测试宏)是C++20中引入的一种强大机制,用于应对兼容性问题。Feature Test Mocros作为预处理器指令(preprocessor directives)出现,它使你能够在编译过程中仔细检查特定语言或库功能(particular language

2025秋招NLP算法面试真题(二)-史上最全Transformer面试题:灵魂20问帮你彻底搞定Transformer

简单介绍 之前的20个问题的文章在这里: https://zhuanlan.zhihu.com/p/148656446 其实这20个问题不是让大家背答案,而是为了帮助大家梳理 transformer的相关知识点,所以你注意看会发现我的问题也是有某种顺序的。 本文涉及到的代码可以在这里找到: https://github.com/DA-southampton/NLP_ability 问题

【LocalAI】(13):LocalAI最新版本支持Stable diffusion 3,20亿参数图像更加细腻了,可以继续研究下

最新版本v2.17.1 https://github.com/mudler/LocalAI/releases Stable diffusion 3 You can use Stable diffusion 3 by installing the model in the gallery (stable-diffusion-3-medium) or by placing this YAML fi

每日文献:2018-02-20

自然选择的分子印迹(精读第一天) 由于最近不知不觉开始涉及群体遗传学,所以准备精读(其实就是原文翻译)一篇review尽力去了解这个我陌生的领域。文章原标题为Molecular Signatures of Natural Selection, 作者Rasmus Nielsen。 简介 群体遗传学数十年来一直被一个问题所困扰,那就是如果在观察物种中存在一个遗传变异,那么应该如何定量得描述

每日一题——Python代码实现PAT乙级1048 数字加密(举一反三+思想解读+逐步优化)五千字好文

一个认为一切根源都是“自己不够强”的INTJ 个人主页:用哲学编程-CSDN博客专栏:每日一题——举一反三Python编程学习Python内置函数 Python-3.12.0文档解读 目录 初次尝试  再次尝试 代码点评 代码结构 时间复杂度 空间复杂度 优化建议 我要更强 优化建议 完整代码及注释 时间复杂度和空间复杂度分析 进一步优化 哲学和编程思想 模块化

【UML用户指南】-20-对基本行为建模-交互图

目录 1、概述 2、顺序图 2.1、两个不同于通信图的特征: 2.1.1、顺序图有对象生命线 2.1.2、顺序图有控制焦点 2.2、结构化控制 2.2.1、可选执行opt 2.2.2、条件执行alt 2.2.3、并行执行par 2.2.4、循环迭代执行loop 2.3、嵌套活动图 3、通信图 3.1、两个不同于顺序图的特征 3.1.1、通信图有路径 3.1.2、通信图