Skip to content

Latest commit

 

History

History
103 lines (85 loc) · 1.88 KB

6174猜想.md

File metadata and controls

103 lines (85 loc) · 1.88 KB

##问题

6174猜想
Description

6174猜想 ,1955年,卡普耶卡(D.R.Kaprekar)研究了对四位数的一种变换:任给出四位数k0,用它的四个数字由大到小重新排列成一个四位数m,再减去它的反序数rev(m),得出数k1=m-rev(m),然后,继续对k1重复上述变换,得数k2.如此进行下去,卡普耶卡发现,无论k0是多大的四位数, 只要四个数字不全相同,最多进行7次上述变换,就会出现四位数6174


Input

输入一个四位数(四个数字不完全相同)


Output

将这个四位数的四个数字重新排列组成一个最大的数和一个最小的数相减,得到一个新的四位数;重复操作,直到最终结果为6174。每行输出一个算式,最后一行输出变换次数。


Sample Input 1 

7888
Sample Output 1

8887-7888=999
9990-999=8991
9981-1899=8082
8820-288=8532
8532-2358=6174
5

代码:

#include<iostream>
using namespace std;
void change(int num[4])
{

	/*for (int i = 0; i < 4; i++)
	{

			for(int j=i+1;j<=4;j++)
		{
				if (num[j] < num[i])
				{
					int temp = 0;
					temp= num[i];
					 num[i] = num[j];
					num[j] = temp;
				}
			
	
			}
	}
*/
	int i, k, tmp;
	for (i = 1; i<4; i++)
	{
		k = i - 1;
		tmp = r[i];
		while (k >= 0 && r[k]>tmp)
		{
			r[k + 1] = r[k];
			k--;
		}
		r[k + 1] = tmp;
	}

}
int main()
{
	int n;
	cin >> n;
	int num[5];
	int s, t;
	int end=0;
	int a, b, c, d;
	int count = 0;
	
	while (end != 6174)
		{
			a = n / 1000;
			b = (n % 1000) / 100;
			c = (n % 100) / 10;
			d = n % 10;
	
				num[0] = a;
				num[1] = b;
				num[2] = c;
				num[3] = d;
		change(num);
		s = num[0] * 1000 + num[1] * 100 + num[2] * 10 + num[3];
		t = num[0] + num[1] * 10 + num[2] * 100 + num[3] * 1000;
		end = t-s;
		cout << s << "-" << t << "=" << end << endl;
		count++;
		n = end;
		/*change(end);*/
	}
	  system("pause");
    return 0;
}
```
注意循环: