1069 The Black Hole of Numbers (20)(20 分)

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

 

需要注意的点是:刚开始输入的数字可能不足四位要自动补0 以后的结果可能也不足四位都要自动补0

否则运行超时 我T在这里

还有就是结果是0或者6147都是要退出的

代码:

#include<bits/stdc++.h>
using namespace std;
char str[6];
bool cmp(char a,char b){
	return a>b;
}
int main()
{
	int n,res;
	scanf("%s",str);
	
	do{
		while(strlen(str) < 4)
			str[strlen(str)] = '0';
		str[4] = '\0';
		
		
		sort(str,str+4);//最小 
		int minn = atoi(str);
		sort(str,str+4,cmp); 
		int maxn = atoi(str);
		
		res = maxn - minn;
		printf("%04d - %04d = %04d\n",maxn,minn,res);
		
		sprintf(str,"%d",res);
		if(res == 0) return 0; 
	}while(res != 6174);
	return 0;	
} 

 

 

还有一份我看网上的虽然有点长不过里面学到了stringstream 这个东西针对string类的 转化的还不错

转载来自:https://blog.csdn.net/qq_37597345/article/details/81299778

#include<cstdio>
#include<vector>
#include<string>
#include<sstream>
#include<algorithm>
using namespace std;
bool cmp(char a, char b){
	return a > b;
}
int main()
{
	int n, min, max;
	string str, smin, smax;
	stringstream ss;
	
	scanf("%d", &n);
	ss << n; ss >> str; ss.clear();
	while(4 - str.size())
		str.push_back('0');	
		
	sort(str.begin(), str.end());
	smin = str;
	sort(str.begin(), str.end(), cmp);
	smax = str;
	ss << smax; ss >> max; ss.clear();
	ss << smin; ss >> min; ss.clear();
	
	if(smin == smax)
		printf("%04d - %04d = 0000\n", max, min);
	else
	{
		while(max - min != 6174)
		{
			if(min != 0)
				printf("%04d - %04d = %04d\n", max, min, max - min); //这个情况其实就是包括 以上当结果为0 的情况 
			else 
				break;
				
			ss << max - min; ss >> str; ss.clear();
			while(4 - str.size())
				str.push_back('0');	
				
			sort(str.begin(), str.end());
			smin = str;
			sort(str.begin(), str.end(), cmp);
			smax = str;
			ss << smax; ss >> max; ss.clear();
			ss << smin; ss >> min; ss.clear();

		}
		if(min != 0)
			printf("%04d - %04d = %04d\n", max, min, max - min);
	}
	return 0;
}

 

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐