1019 数字黑洞(20 分)
给定任一个各位数字不完全相同的 4 位正整数,如果我们先把 4 个数字按非递增排序,再按非递减排序,然后用第 1 个数字减第 2 个数字,将得到一个新的数字。一直重复这样做,我们很快会停在有“数字黑洞”之称的 6174
,这个神奇的数字也叫 Kaprekar 常数。
例如,我们从6767
开始,将得到
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
7641 - 1467 = 6174
... ...
现给定任意 4 位正整数,请编写程序演示到达黑洞的过程。
输入格式:
输入给出一个 (0,104) 区间内的正整数 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
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
//从大到小排列
int cmp(int a, int b)
{
return a > b;
}
//补全0
string zeros(int temp)
{
string c;
if (temp >= 1000)
c = to_string(temp);
else if (temp >= 100 && temp < 1000)
{
c = "0" + to_string(temp);
}
else if (temp >= 10 && temp < 100)
{
c = "00" + to_string(temp);
}
else if (temp >= 1 && temp < 10)
{
c = "000" + to_string(temp);
}
return c;
}
//输出相减结果
string result(string a)
{
string b = a;
int temp;
string c;
sort(a.begin(), a.end(), cmp);
sort(b.begin(), b.end());
temp = stoi(a) - stoi(b);
c = zeros(temp);//相减完有可能不足4位
cout << a << " - " << b << " = " << c << endl;
return c;
}
int main()
{
int n;
string s;
string r = "0";
cin >> n;
s = zeros(n);//输入有可能不足4位
if (s[0] == s[1] && s[0] == s[2] && s[0] == s[3])
cout << s << " - " << s << " = 0000";
else
{
while (r != "6174" && r != "0000")
{
r = result(s);
s = r;
}
}
system("pause");
return 0;
}
优化:
1、 c.insert(0, 4 - c.length(), '0'); //用来给不足4位的时候前面补0
string &insert(int p0, int n, char c);//在p0处插入n个字符c
string的成员函数insert有以下多种重载:
string &insert(int p0, const char *s);——在p0位置插入字符串s
string &insert(int p0, const char *s, int n);——在p0位置插入字符串s的前n个字符
string &insert(int p0,const string &s);——在p0位置插入字符串s
string &insert(int p0,const string &s, int pos, int n);——在p0位置插入字符串s从pos开始的连续n个字符
string &insert(int p0, int n, char c);//在p0处插入n个字符c
iterator insert(iterator it, char c);//在it处插入字符c,返回插入后迭代器的位置
void insert(iterator it, const_iterator first, const_iteratorlast);//在it处插入从first开始至last-1的所有字符
void insert(iterator it, int n, char c);//在it处插入n个字符c
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
//从大到小排列
int cmp(int a, int b)
{
return a > b;
}
//输出相减结果
string result(string a)
{
string b = a;
int temp;
string c;
sort(a.begin(), a.end(), cmp);
sort(b.begin(), b.end());
temp = stoi(a) - stoi(b);
c = to_string(temp);
//c = zeros(temp);//相减完有可能不足4位
c.insert(0, 4 - c.length(), '0');
cout << a << " - " << b << " = " << c << endl;
return c;
}
int main()
{
string s;
string r = "0";
cin >> s;
s.insert(0, 4 - s.length(), '0');
if (s[0] == s[1] && s[0] == s[2] && s[0] == s[3])
cout << s << " - " << s << " = 0000";
else
{
while (r != "6174" && r != "0000")
{
r = result(s);
s = r;
}
}
system("pause");
return 0;
}
可以去掉if else
int main()
{
string s;
string r = "0";
cin >> s;
s.insert(0, 4 - s.length(), '0');
while (r != "6174" && r != "0000")
{
r = result(s);
s = r;
}
system("pause");
return 0;
}