ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

PAT 甲级 1069 The Black Hole of Numbers (20 分)(内含别人string处理的精简代码)

2019-10-31 13:01:12  阅读:302  来源: 互联网

标签:1069 digit PAT string int number 6174 9621


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.

Input Specification:

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

Output Specification:

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.

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
作者: CHEN, Yue 单位: 浙江大学 时间限制: 200 ms 内存限制: 64 MB 代码长度限制: 16 KB

 

题意:

给一个数组n,先对各个数字位从大到小排序得到a,然后逆置得到b,计算a-b,得到c,重复上述过程,直到得到6174,或者,对于特例,四个数字位完全相同,那么输出0000。

题解:

刚开始输入的数字可能不足四位要自动补0 以后的结果可能也不足四位都要自动补0

结果是0或者6174都是要退出的

一开始没考虑到6174,测试点5没过,后来想到了

AC代码:

#include<iostream>
#include<algorithm>
using namespace std;
int n;
int a[10];
int main(){
     cin>>n;
     int big=0;
     int small=0;
     int x=n;
     int k=0,f;
     if(x==6174){
        printf("7641 - 1467 = 6174");
        return 0;
     }
     while(x!=6174){
         k=0;
         big=0;
         small=0;
         while(x>=10){
             a[++k]=x%10;
             x/=10;
         }
         a[++k]=x;
         while(k<4){
             a[++k]=0;
         }
         sort(a+1,a+1+4);
         for(int i=1;i<=4;i++){
             big=big*10+a[5-i];
             small=small*10+a[i];
         }
         x=big-small;
         printf("%04d - %04d = %04d\n",big,small,x);
         if(x==0) break;
     }
     return 0;
}
     

 

更简洁的string处理的代码:

#include<bits/stdc++.h>
using namespace std;
bool cmp(char a,char b) 
{
    return a>b;
}
int main(void)
{
    string s;
    cin>>s;
    s.insert(0,4-s.size(),'0');
    do{
        string a=s,b=s;
        sort(a.begin(),a.end(),cmp);
        sort(b.begin(),b.end());
        int diff=stoi(a)-stoi(b);
        s=to_string(diff);
        s.insert(0,4-s.size(),'0');
        cout<<a<<" - "<<b<<" = "<<s<<endl;
    }while(s!="6174"&&s!="0000");
    return 0;

————————————————
版权声明:本文为CSDN博主「Imagirl1」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Imagirl1/article/details/82261213

 

标签:1069,digit,PAT,string,int,number,6174,9621
来源: https://www.cnblogs.com/caiyishuai/p/11770567.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有