当前位置:网站首页>Pat class B 1019 digital black hole (20 points) (possible causes of measurement point errors)
Pat class B 1019 digital black hole (20 points) (possible causes of measurement point errors)
2022-07-22 18:54:00 【Now there is no forgotten thought】
1019 Digital black hole (20 branch )
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,104) 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:
6767
No blank lines at the end
sample output 1:
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
No blank lines at the end
sample input 2:
2222
No blank lines at the end
sample output 2:
2222 - 2222 = 0000
No blank lines at the end
This is my initial code :
#include <iostream>
#include <algorithm>
using namespace std;
int max(int a[])
{
return a[3]*1000+a[2]*100+a[1]*10+a[0];
}
int min(int a[])
{
return a[0]*1000+a[1]*100+a[2]*10+a[3];
}
void to_array(int n,int a[])
{
int cnt=0;
while (n) {
a[cnt++]=n%10;
n/=10;
}
}
int main()
{
int n;
cin>>n;
int a[100];
while (1) {
to_array(n,a);
sort(a,a+4);
int maxn=max(a);int minn=min(a);
n=maxn-minn;
printf("%04d - %04d = %04d\n",maxn,minn,n);
if (n==0||n==6174) break;
}
}
There is a problem that local measuring points cannot pass , Thought for a long time , It is found that you can see from the input instructions in the title , The input is not necessarily 4 digit ,0-10000 We should meet all the requirements , For two digits 24, It should be regarded as 0024 Look at , After two steps of operation, we get 6714;
So which section of the corresponding code is wrong ? stay int to array In the conversion function of !! Revised as follows :
void to_array(int n,int a[])
{
// int cnt=0;
// while (n) { // Can't calculate 0 until , If it's a two digit number , Should be automatically added to the array 0;
// a[cnt++]=n%10;
// n/=10;
// }
for (int i=0;i<4;i++) {
a[i]=n%10;
n/=10;
}
}
The effect of front and back operation is as follows :
Before the change :
24
-1119662172 - 7470512 = -1127132684
-2468 - -8642 = 6174
--------------------------------
Process exited after 1.877 seconds with return value 0
Please press any key to continue . . .
After modification :
24
4200 - 0024 = 4176
7641 - 1467 = 6174
--------------------------------
Process exited after 1.366 seconds with return value 0
Please press any key to continue . . .
边栏推荐
- 1.moveToThread的一个完整实例,2.qt表达式求值
- PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/mysqli.so'
- 数据优化的方式
- 消息中间件
- Protocol and port
- JSON序列化对象时,如何返回有空值的带属性名称json字符串?
- 数据存储分区--范围分区,哈希分区,列表分区,性能调优必不可缺少的部分
- Wampserver is built to Tencent cloud service. Localhost can access Apache, and the domain name can also access its own IIS server, but the domain name cannot be accessed
- App mobile End test [6] application (APK) package Management and Activity
- Strncpy() copy string (limited by length)
猜你喜欢
Uniapp wechat applet map, open in Gaode app, Tencent app, baidu app, apple map app
LeetCode: 185. 部门工资前三高的所有员工
1.雷点:mysql数据库转移到oracle,2.qt5.12.3 连接oracle 12c数据库
win10sp1升到最新版本;QT5.9.6静态编译(network有效)
PTA基础题 7-23 币值转换 (20 分) (属实恶心)
PTA 基础题7-28猴子选大王(简单方法)
Data Lake simple record
LeetCode 2028. 找出缺失的观测数据
Leetcode 304. two dimensional area and retrieval - matrix immutable
How to resolve errors in executing the yum makecache command
随机推荐
分库分表
NRF24L01无线模块设置发射接受模式方法
Leetcode 116. fill in the next right node pointer of each node
webFlux 中使用 openFeign 实现远程调用遇到的问题
IP address, CIRD format URL, hostname regular expression
Leetcode 2028. find out the missing observation data
模块TensorFlow中没有Session
There is no session in the tensorflow module
Summary 20220120
Flink学习笔记(七)处理函数
LeetCode: 627. 变更性别
Go language learning: go language journey (4)
LeetCode: 197. 上升的温度
LeetCode 693. 交替位二进制数
OSI model, tcp/ip model
Leetcode 720. the longest word in the dictionary
PAT基础习题集7-26 单词长度(精简代码)
04. interface aggregation principle
05. Law of Demeter LOD
Unity: 快速定位摄像机Camera