1069. The Black Hole of Numbers (20) -----C++

来源:互联网 发布:网络视听审核员证书 编辑:程序博客网 时间:2024/05/19 03:24

题目描述
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 (0, 10000).

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
题目分析
对于所有的不是全部相同的4位数,进行升序和降序排列,再将排列后的两个进行相减,可以得到一个新数。知道最终这个新数字不会再变化,就称为数字黑洞。再对数字进行排序是不足4位的,升序的前面加0,降序的后面加0.如果这4位相同,按照格式直接输出。
题目难点
1.用纯数字好不好进行处理?不好进行处理,尤其在升序和降序的时候,所有我采用字符串。用sort函数进行排序
1.不足4位补0。首先补0操作是在对字符串升序和降序后进行处理的,而且分清楚是前面加0还是后面加0,如果是降序的是后面加0,升序的是前面加0.
2.升序和降序排列。我采用单独的函数。直接调用sort(s.begin(),s.end())。第一次的时间对STL容器函数理解不深入,因此只会升序不会降序,采用了笨方法,即升序后从后向前进行一遍字符串拼接操作。第二次查阅了资料找到了反向迭代器,采用sort(s.rbegin(),s.rend())即可。
3.判断是不是相同的4位数,对升序和降序后的字符串比较,相等则相同,否则则不同。
代码

#include <iostream>#include <string>#include <algorithm>#include <sstream>/* run this program using the console pauser or add your own getch, system("pause") or input loop */using namespace std;int strtoint(string s){//字符串转数字     stringstream ss;    ss<<s;    int res;    ss >> res;    return res;}string inttostr(int s){//数字转字符串     stringstream ss;    ss<<s;    string res;    ss >> res;    return res;}string incre(string s){//升序     sort(s.begin(),s.end());    return s;}string decre(string str){//降序     //string s = incre(str);//麻烦的方式 //  string res = "";//  for(int i=s.length()-1;i>=0;i--){//      res = res+s.substr(i,1);//  }    sort(str.rbegin(),str.rend());    return str;}string tofour(string s,int num){//不够4位转4位     int len = s.length();    if(num==0){//降序的转         while(len<4){            s = s+"0";            len = s.length();        }    }    else if(num==1){//升序的转         while(len<4){            s = "0"+s;            len = s.length();        }    }    return s;}int main(int argc, char *argv[]) {    string s,destr,instr;    cin>>s;    int num1, num2;    if(destr.length()<4){        destr = tofour(decre(s),0);//先调用降序函数再转4位     }    if(instr.length()<4){//先调用升序函数再转4位         instr = tofour(incre(s),1);    }    num1 = strtoint(destr);//转数字     num2 = strtoint(instr);    //cout<<decre(s)<<" "<<incre(s)<<endl;    if(destr==instr){        cout<<s<<" - "<<s<<" = 0000"<<endl;        return 0;    }    else{        int t = num1 - num2;        string temp;        while(t!=6174){//不是6174循环             temp = inttostr(t);            if(temp.length()){                temp = tofour(temp,1);            }            cout<<destr<<" - "<<instr<<" = "<<temp<<endl;            s = inttostr(t);            destr = tofour(decre(s),0);            instr = tofour(incre(s),1);            num1 = strtoint(destr);            num2 = strtoint(instr);             t = num1-num2;        }        cout<<destr<<" - "<<instr<<" = "<<t<<endl;//等于6174的第一次必须输出     }    return 0;}
原创粉丝点击