Bzoj1666: [usaco 2006 Oct] Another cow number game dairy digital game

Source: Internet
Author: User
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

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.