9 degrees OJ 1031 xxx law, 9 degrees oj1031xxx
Question 1031: xxx Law
Time Limit: 1 second
Memory limit: 32 MB
Special question: No
Submit: 4995
Solution: 3187
-
Description:
-
If n is an even number, n is halved. If n is an odd number, n is converted to 3 * n + 1 and then halved until the number is changed to 1.
Perform several steps to change n to 1. For details, see the example.
-
Input:
-
The test contains multiple use cases. Each use case contains an integer of n. If n is 0, the input ends. (1 <= n <= 10000)
-
Output:
-
For each group of test cases, please output a number, indicating the number of steps to pass, each group of output occupies one line.
-
Sample input:
-
310
-
Sample output:
-
50
#include<stdio.h>int main(int argc, char *argv[]){ int n; int i=0; while(scanf("%d",&n)!=EOF) { i=0; if(n==0)break; while(n!=1){ if(n%2==0) { n=n/2; i++; } else if(n%2==1) { n=3*n+1; n=n/2; i++; } } if(n==1) printf("%d\n",i); } return 0;} /************************************************************** Problem: 1031 User: kirchhoff Language: C Result: Accepted Time:0 ms Memory:912 kb****************************************************************/