1666: [usaco Oct] Another cow number game dairy's digital game time limit: 5 sec memory limit: 64 MB
Submit: 502 solved: 432
[Submit] [Status] Description
The cows are playing a boring digital game. Bessie, who is very depressed, wants to ask you to write a program to help her predict the results at the beginning. At the beginning of the game, each ox gets a number of N (1 <=n <= 1,000,000 ). At this time, the cows scored 0. If n is an odd number, the cow will multiply it by 3 and then add 1. If n is an even number, the number is divided by 2. Every time the number changes, the cow gets 1 point. When the value of N is equal to 1, the game ends and the score is the final score of the cow in this game. The following is the complete game process when the initial value of N is 5: n operation after the number of comments total score 5 16 3*5 + 1 1 16 8 16/2 2 8 4 8/2 3 4 2 4/2 4 2 1 2/2 5 the final score of this cow is 5.
Input
* Row 1st: a positive integer, n
Output
* Row 1st: Output a positive integer N, that is, the final score of the cows in this game.
Sample input112
Sample output20hint Source
Gold
Question:
3N + 1 problem...
Code:
1 var n,ans:longint; 2 begin 3 assign(input,‘input.txt‘);assign(output,‘output.txt‘); 4 reset(input);rewrite(output); 5 readln(n);ans:=0; 6 while n<>1 do 7 begin 8 inc(ans); 9 if odd(n) then n:=3*n+1 else n:=n>>1;10 end;11 writeln(ans);12 close(input);close(output);13 end.
View code