數(shù)學(xué)黑洞
Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic PRoblem Description
任意一個(gè)4位自然數(shù)N(N不能是4個(gè)數(shù)字一樣,如1111、2222、….9999是不可以的,N也不能是6174),將組成自然數(shù)N的4個(gè)數(shù)字重新排列,形成一個(gè)最大數(shù)和最小數(shù),最大數(shù)和最小數(shù)相減,其差是還是自然數(shù),將差的各數(shù)字再重新排列,又形成一個(gè)最大數(shù)和最小數(shù),最大數(shù)和最小數(shù)相減,其差還是自然數(shù)。反復(fù)進(jìn)行,直到差是一個(gè)神秘?cái)?shù)6174(數(shù)學(xué)黑洞)結(jié)束。
Input
輸入數(shù)據(jù)有多組,每組占一行,每行包含一個(gè)4位自然數(shù)N。輸入文件直到EOF為止!
Output
對(duì)每組輸入,輸出有2行。第一行是所有差,以空格分隔,最后一個(gè)數(shù)后也有空格;第二行是差的個(gè)數(shù)。
Example Input
1000 1500 3000
Example Output
999 8991 8082 8532 6174
5
5085 7992 7173 6354 3087 8352 6174
7
2997 7173 6354 3087 8352 6174
6
#include <bits/stdc++.h>using namespace std;int s,a[10];void f( int n){ int i=1; while(n) { a[i++]=n%10; n=n/10; } sort(a,a+i); int y,x; x=a[1]*1000+a[2]*100+a[3]*10+a[4]; y=a[4]*1000+a[3]*100+a[2]*10+a[1]; int m=y-x; s++; cout<<m<<' '; if(m<1000) m*=10; if(m==6174) { cout<< endl << s << endl; return; } f(m);}int main(){ int n; while (cin>>n) { s=0; f(n); } return 0;}新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注