1019 数字黑洞

给定任一个各位数字不完全相同的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或者0000结束。
代码如下:
 #include<iostream>
#include<string>
#include<algorithm> using namespace std; bool cmp(const char &a,const char &b)
{
return a>b;
} int main()
{
string n, n1, n2, n3;
int ac = ;
cin>>n;
/* 对于不满4位的数据前面补0 */
if( n.length() == ) n = "" + n;
else if( n.length() == ) n = "" + n;
else if( n.length() == ) n = "" + n; while( n != ""|| ac){
ac = ;
sort(n.begin(), n.end(),cmp);
n1 = n;
sort(n.begin(),n.end());
n2 = n;
if(n1 == n2){
cout<<n1<<" - "<<n2<<" = "<<""<<endl;
break;
}
n3 = n1;
for( int i = n.length(); i >= ; i--){
if(n3[i] < n2[i]){
n3[i-]--;
n3[i] += ;
}
n[i] = n3[i] - n2[i] + '';
}
cout<<n1<<" - "<<n2<<" = "<<n<<endl;
}
return ;
}
 

PAT Basic 1019的更多相关文章

  1. PAT Basic 1019 数字黑洞 (20 分)

    给定任一个各位数字不完全相同的 4 位正整数,如果我们先把 4 个数字按非递增排序,再按非递减排序,然后用第 1 个数字减第 2 个数字,将得到一个新的数字.一直重复这样做,我们很快会停在有“数字黑洞 ...

  2. PAT Basic 1057

    1057 数零壹 给定一串长度不超过 10​5​​ 的字符串,本题要求你将其中所有英文字母的序号(字母 a-z 对应序号 1-26,不分大小写)相加,得到整数 N,然后再分析一下 N 的二进制表示中有 ...

  3. PAT (Basic Level) Practise (中文)-1039. 到底买不买(20)

    PAT (Basic Level) Practise (中文)-1039. 到底买不买(20) http://www.patest.cn/contests/pat-b-practise/1039 小红 ...

  4. PAT (Basic Level) Practise (中文)- 1022. D进制的A+B (20)

    PAT (Basic Level) Practise (中文)-  1022. D进制的A+B (20)  http://www.patest.cn/contests/pat-b-practise/1 ...

  5. PAT (Basic Level) Practise (中文)- 1024. 科学计数法 (20)

    PAT (Basic Level) Practise (中文)- 1024. 科学计数法 (20) http://www.patest.cn/contests/pat-b-practise/1024 ...

  6. PAT (Basic Level) Practise (中文)-1025. 反转链表 (25)

    PAT (Basic Level) Practise (中文)-1025. 反转链表 (25)   http://www.patest.cn/contests/pat-b-practise/1025 ...

  7. PAT (Basic Level) Practise (中文)- 1026. 程序运行时间(15)

    PAT (Basic Level) Practise (中文)- 1026. 程序运行时间(15)    http://www.patest.cn/contests/pat-b-practise/10 ...

  8. PAT (Basic Level) Practise (中文)-1027. 打印沙漏(20)

    PAT (Basic Level) Practise (中文)-1027. 打印沙漏(20)  http://www.patest.cn/contests/pat-b-practise/1027 本题 ...

  9. PAT (Basic Level) Practise (中文)-1028. 人口普查(20)

    PAT (Basic Level) Practise (中文)-1028. 人口普查(20)   http://www.patest.cn/contests/pat-b-practise/1028 某 ...

随机推荐

  1. Linux--NiaoGe-Service-07网络安全与主机基本防护

    Linux系统内自带的防火墙有两层: 第一层:数据包过滤防火墙:IP Filtering和Net Filter 要进入Linux本机的数据包都会先通过Linux预先内置的防火墙(Net Filter) ...

  2. Nginx 开启目录浏览功能配置

    在server节点下添加 server { listen ; server_name default; #index index.php; # 目录浏览功能 autoindex on; # 显示文件大 ...

  3. 关于php和docker

    Docker在PHP项目开发环境中的应用 http://www.wolonge.com/zhuanlan/detail/117441 Docker在PHP项目开发环境中的应用 http://linux ...

  4. CF1066E Binary Numbers AND Sum

    思路: 模拟.实现: #include <iostream> using namespace std; ; ], b[]; ]; int main() { int n, m; while ...

  5. 提高VS2010运行速度的技巧+关闭拼写检查

    任务管理器,CPU和内存都不高,为何?原因就是VS2010不停地读硬盘导致的; 写代码2/3的时间都耗在卡上了,太难受了; 研究发现,VS2010如果你装了VC等语言,那么它就会自动装SQL Serv ...

  6. 指定ip地址登陆服务器

    [root@localhost ~]# cat /etc/hosts.allow ## hosts.allow   This file contains access rules which are ...

  7. window服务的使用

    目前的项目中使用很多服务来进行实现.服务是依靠windows操作系统来实现.可以是定时器类型,比如定时执行费时的任务,这种任务时最多.也可以是一些服务(SOAP)的宿主,不在限制与iis,不现在限制于 ...

  8. vs2010调试sql2008存储过程

      1.安装vs2010sp1补丁 2.vs中打开服务器资源管理器,并进行数据库连接,连接时要注意 3. 4.可以打开数据库中的存储过程进行调试了

  9. hihoCode r#1077 : RMQ问题再临-线段树

    思路: 两种实现方法: (1)用链表(2)用数组. #include <bits/stdc++.h> using namespace std; int n, q, L, R, op, P, ...

  10. Install your Application into RaspberryPi2 and automatically start up

    如何安装和卸载应用程序到RaspberryPi2? 如何配置应用程序在RaspberryPi2开机后自动启动? How to install your app into RaspberryPi2? H ...