ZOJ Problem Set - 2722 Head-to-Head Match
来源:互联网 发布:身份证读卡器软件下载 编辑:程序博客网 时间:2024/06/02 01:04
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.
InputThe 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.
OutputFor each test case, output the number of rounds needed in the contest, on a single line.
Sample Input816150Sample 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;
}
- ZOJ Problem Set - 2722 Head-to-Head Match
- zoj 2722 Head-to-Head Match
- ZOJ 2722 Head-to-Head Match
- ZOJ 2722 Head-to-head match
- zoj 2722 Head-to-Head Match
- zoj 2722 Head-to-Head Match (睡前一水~)
- ZOJ2722Head-to-Head Match
- head
- head
- head
- head
- HEAD
- HEAD
- fopen、file_get_contents方式set head
- ZOJ Problem Set - 3878||Convert QWERTY to Dvorak
- ZOJ Problem Set - 2060
- ZOJ Problem Set - 2972
- ZOJ Problem Set - 1037
- java 静态代理和动态代理
- Android 平台 万年历 应用
- android显示系统分析
- 修改Android系统源代码,优化开机速度。
- 不可变实例、可变实例、copy与mutableCopy
- ZOJ Problem Set - 2722 Head-to-Head Match
- 【Android API简述】android.view.View.OnClickListener
- 抽象工厂
- 玖笆淤淹钦粟陀撼蒂上喜方那驶炭
- Android Toast
- jquery获取浏览器高度和宽度
- Qt新渲染底层Scene Graph研究(二)
- HDU 1421 搬寝室
- linux内核抢占与spinlock