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:6767Sample Output 1:
7766 - 6677 = 1089 9810 - 0189 = 9621 9621 - 1269 = 8352 8532 - 2358 = 6174Sample Input 2:
2222Sample Output 2:
2222 - 2222 = 0000
分析:
题目简单,但是有很多坑;比如:
(1)
输入2222
2222 - 2222 = 0000
但是输入 222
(2)输入 10000
所以在循环过程中需要不断判断,所得到的的数是否由一个数字构成。
代码:
#include<iostream> #include<string> #include<string.h> #include<vector> #include<algorithm> #include<set> using namespace std; int main(){ char input[7]; cin>>input; int i,j; vector<int> vec; set<int> s; for(i=0; i<strlen(input); i++){ vec.push_back( input[i] - '0' ); s.insert(input[i] - '0');//利用set自动去重 } while(vec.size()<4){ vec.push_back(0); s.insert(0); } //排序 sort(vec.begin(),vec.end()); int a=0,b=0; int t = 10; for(i=0,j=vec.size()-1; i<vec.size() && j>=0; i++,j--){ a = a*10 + vec[i]; b = b*10 + vec[j]; } //如果输入的数字仅由一个数字构成 if(s.size() == 1 ){ printf("%04d - %04d = 0000\n",a,a); return 0; } while( (b-a) != 6174){ int t = b-a; printf("%04d - %04d = %04d\n",b,a,t); vector<int> v; while(t != 0){ v.push_back( t%10 ); t /= 10; } while(v.size() < 4){ v.push_back(0); } sort(v.begin(),v.end()); a = 0; b = 0; set<int> set_temp; for(i=0,j=v.size()-1; i<v.size() && j>=0; i++,j--){ a = a*10 + v[i]; b = b*10 + v[j]; set_temp.insert(v[i]); } if(set_temp.size() == 1){ printf("%04d - %04d = 0000\n",a,a); return 0; } } printf("%04d - %04d = %04d\n",b,a,b-a); return 0; }
解法二:
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <set> using namespace std; bool cmp(int a, int b){ return a>b; } int vec2int(vector<int> vec){ int i,total=0; for(i=0; i<vec.size(); i++){ total = total*10 + vec[i]; } return total; } void int2vec(int s, vector<int> &vec){ int i; while(s!=0){ vec.push_back(s%10); s/=10; } while(vec.size()<4){ vec.push_back(0); } sort(vec.begin(), vec.end(), cmp); } int main(int argc, char** argv) { string str; cin>>str; vector<int> vec; int i; for(i=0; i<str.size(); i++){ vec.push_back(str[i]-'0'); } if(vec.size()==4 && vec[0]==vec[1] && vec[0]==vec[2] && vec[0]==vec[3]){ cout<<str<<" - "; reverse(str.begin(), str.end()); cout<<str<<" = "<<"0000"<<endl; return 0; } while(vec.size()<4){ vec.push_back(0); } sort(vec.begin(), vec.end(), cmp); int a, b, result; while(result != 6174){ a = vec2int(vec); reverse(vec.begin(), vec.end()); b = vec2int(vec); result = a-b; printf("%04d - %04d = %04d\n",a,b,result); vec.clear(); int2vec(result,vec); } return 0; }