The Black Hole of Numbers (strtoint+inttostr+sort)

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

坑点1、数字都要四位的2、如果是判断下一个式子的差不等于上个结果,然后跳出的话,那么需要判断,是不是第一次输出。比如,输入6174,结果就等于6174,那么就会没输出,直接跳出。

  1 #include <iostream>
  2 
  3 #include <algorithm>
  4 
  5 #include<string>
  6 
  7 #include <sstream>
  8 
  9 #include <iomanip>
 10 
 11 using namespace std;
 12 
 13  
 14 
 15 int a1[5];
 16 
 17 int a2[5];
 18 
 19  
 20 
 21 int bb[1000];
 22 
 23  
 24 
 25 bool cmp1(int a,int b)
 26 
 27 {
 28 
 29    return a>b;
 30 
 31 }
 32 
 33  
 34 
 35 bool cmp2(int a,int b)
 36 
 37 {
 38 
 39    return a<b;
 40 
 41 }
 42 
 43  
 44 
 45 int main()
 46 
 47 {
 48 
 49       string n;
 50 
 51       int i;
 52 
 53  
 54 
 55       while(cin>>n)
 56 
 57       {
 58 
 59       
 60 
 61          int tt,c1,c2;
 62 
 63          stringstream ss1;
 64 
 65          ss1<<n;
 66 
 67          ss1>>tt;
 68 
 69       
 70 
 71          i=0;
 72 
 73          bool fir=true;
 74 
 75          while(true)
 76 
 77          {
 78 
 79  
 80 
 81                   string ss;
 82 
 83                   stringstream ss2;
 84 
 85                   ss2<<setfill('0')<<setw(4)<<tt;
 86 
 87                   ss2>>ss;
 88 
 89  
 90 
 91                   for(i=0;i<ss.length();i++)
 92 
 93                   {
 94 
 95                  a1[i]=ss[i]-'0';
 96 
 97                    a2[i]=a1[i];
 98 
 99                   }
100 
101  
102 
103              sort(a1,a1+ss.length(),cmp1);
104 
105                sort(a2,a2+ss.length(),cmp2);
106 
107  
108 
109                    c1=0; c2=0;
110 
111                for(i=0;i<ss.length();i++)
112 
113                   {
114 
115                    c1=c1*10+a1[i];
116 
117                      c2=c2*10+a2[i];
118 
119                   }
120 
121  
122 
123                   
124 
125                   if(c1-c2==tt&&!fir)  break;
126 
127                   else 
128 
129                         
130 
131                   {
132 
133                         fir=false;
134 
135                         cout<<setfill('0')<<setw(4)<<c1<<" - "<<setfill('0')<<setw(4)<<c2<<" = "<<setfill('0')<<setw(4)<<c1-c2<<endl;
136 
137                         tt=c1-c2;
138 
139                   }     
140 
141          }  
142 
143       }
144 
145    return 0;
146 
147 }
148 
149  
View Code
原文地址:https://www.cnblogs.com/xiaoyesoso/p/4235157.html