6174 Problem time limit:MS | Memory limit:65535 KB Difficulty:2
-
Describe
-
Let's say you have a four-digit number that each has a different number, you get a after all the numbers from the big to the small, you get B from small to large, and then you replace the number with a-a, and you continue to do so. For example, starting from 1234, you can get 4321-1234=3087, 8730-378=8352, 8532-2358=6174, and then back to its own! Now you need to write a program to determine how many times a four-digit number of such operations can occur, and the number of times the operation is calculated
For example, input 1234 execution order is 1234->3087->8352->6174->6174, output is 4
-
-
Input
-
-
the first line is N, which represents the n set of test data.
The next n lines each write a four-digit number that each of you has a different number.
-
-
Output
-
-
How many times does the action described above appear to loop
-
-
Sample input
-
-
11234
-
-
Sample output
-
4
1#include <iostream>2#include <algorithm>3#include <string.h>4 using namespacestd;5 Chara[5],b[5];intIa,ib;6 7 intMain ()8 {9 intn,count,temp=0;TenCin>>N; One while(n--) A { -Count=0; - for(intI=0;i<4; i++) theCin>>A[i]; - Loop: -ia=ib=0; -Sort (a,a+4); + for(intI=0;i<4; i++) - { +ia*=Ten; Aia+= (a[i]-'0'); at } - for(intI=3; i>=0; i--) - { -ib*=Ten; -ib+= (a[i]-'0'); - } inib=ib-ia; -count++; to if(temp!=IB) + { -temp=IB; the for(intI=0;i<4; i++) * { $A[i]= (ib%Ten+'0');Panax NotoginsengIb/=Ten; - } the GotoLoop; + } A Else the { +cout<<count<<Endl; - } $ } $ -}
1#include <iostream>2#include <algorithm>3#include <stdio.h>4 using namespacestd;5 intMain ()6 {7 intK;8Cin>>K;9 while(k--)Ten { One intn,a[4],n1,n2; Ascanf"%d",&n); - ints=1; - while(n!=6174) the { -a[0]=n%Ten; -a[3]=n/ +; -a[1]=n/Ten%Ten; +a[2]=n/ -%Ten; -Sort (a,a+4); +n1= +*a[3]+ -*a[2]+Ten*a[1]+a[0]; AN2= +*a[0]+ -*a[1]+Ten*a[2]+a[3]; atN=n1-N2; -s++; - } -printf"%d\n", s); - } -}
Nyoj 57 6174 Questions