ZOJ Problem Set - 2722 Head-to-Head Match

来源:互联网 发布:身份证读卡器软件下载 编辑:程序博客网 时间:2024/06/02 01:04
Head-to-Head Match

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Our school is planning to hold a new exciting computer programming contest. During each round of the contest, the competitors will be paired, and compete head-to-head. The loser will be eliminated, and the winner will advance to next round. It proceeds until there is only one competitor left, who is the champion. In a certain round, if the number of the remaining competitors is not even, one of them will be chosed randomly to advance to next round automatically, and then the others will be paired and fight as usual. The contest committee want to know how many rounds is needed to produce to champion, then they could prepare enough problems for the contest.

Input

The input consists of several test cases. Each case consists of a single line containing a integer N - the number of the competitors in total. 1 <= N <= 2,147,483,647. An input with 0(zero) signals the end of the input, which should not be processed.

Output

For each test case, output the number of rounds needed in the contest, on a single line.

Sample Input
816150
Sample Output
344


Author: YANG, Chao

Source: Zhejiang University Local Contest 2006, Preliminary 



分析:

题意:

给若干组数,每组数只有一个数。若这个数是偶数,除以2(右移一位);否则+1后再除以2.。问:经过多少次右移操作后这个数刚好是1?



水题。

ac代码:

#include <iostream>
#include<cstdio>
using namespace std;


int main()
{
    long n;//不能用int,因为万一这个数刚好是2147483647,是个奇数,必须加1,这时就超出了int的最大值了
    int i;
    while(scanf("%ld",&n)&&n)
    {
        for(i=0;n>1;i++)
        {
            if(n%2)
            n=n+1;//2147483647+1超出int的最大值
            n=n>>1;
            //printf("%d ",n);
        }


        printf("%d\n",i);
    }
    return 0;
}

0 0
原创粉丝点击