數學黑洞
Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic PRoblem Description
任意一個4位自然數N(N不能是4個數字一樣,如1111、2222、….9999是不可以的,N也不能是6174),將組成自然數N的4個數字重新排列,形成一個最大數和最小數,最大數和最小數相減,其差是還是自然數,將差的各數字再重新排列,又形成一個最大數和最小數,最大數和最小數相減,其差還是自然數。反復進行,直到差是一個神秘數6174(數學黑洞)結束。
Input
輸入數據有多組,每組占一行,每行包含一個4位自然數N。輸入文件直到EOF為止!
Output
對每組輸入,輸出有2行。第一行是所有差,以空格分隔,最后一個數后也有空格;第二行是差的個數。
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;}新聞熱點
疑難解答