for any natural number N0, first add the sum of its numbers, then multiply the sum by 3 and add 1 to become a new natural number N = 1, and then repeat this operation for N1 to produce a new natural number N2. Repeat this operation many times, and the result will be a fixed number Nk, which is like falling into a number "trap". This question requires that the natural number of input be given the process of falling into the "trap".
input format:
give a natural number N0 (N0 < 30000) on one line.
output format:
for input N"0, output the steps that fall into the trap line by line. Line I describes step I of N falling into a trap, in the format: iRV N falling i (i 1). When the result of the natural number obtained in a certain step Nk (k 1) is the same as the previous step Nk-1, stop the output.
my code is as follows:
-sharpinclude<stdio.h>
int xian=0;
int trap(int a){
int wei=1,x,b,sum=0;
x=a;
while(x>9){
x/=10;
wei*=10;
}
do{
b=a/wei;
sum+=b;
a%=wei;
wei/=10;
}while(wei>0);
xian=sum*3+1;
return xian;
}
int main(){
int a,b=0,shu=1,c;
int trap(int a);
scanf("%d",&a);
c=trap(a);
printf("%d:%d\n",shu,c);
while(b!=c){
b=c;
c=trap(xian);
shuPP;
printf("%d:%d\n",shu,xian);
}
return 0;
}