首页 > ACM题库 > HDU-杭电 > HDU 1196 Lowest Bit-树状数组-[解题报告] C++
2013
12-04

HDU 1196 Lowest Bit-树状数组-[解题报告] C++

Lowest Bit

问题描述 :

Given an positive integer A (1 <= A <= 100), output the lowest bit of A.

For example, given A = 26, we can write A in binary form as 11010, so the lowest bit of A is 10, so the output should be 2.

Another example goes like this: given A = 88, we can write A in binary form as 1011000, so the lowest bit of A is 1000, so the output should be 8.

输入:

Each line of input contains only an integer A (1 <= A <= 100). A line containing "0" indicates the end of input, and this line is not a part of the input data.

输出:

For each A in the input, output a line containing only its lowest bit.

样例输入:

26
88
0

样例输出:

2
8

//树状数组入门题

贴上代码饼干

#include<iostream>
using namespace std;
int main(){int n,ans;
while(cin>>n){
	if(n==0)break;
		ans=1;
		while(n%2==0){
			n/=2;
			ans*=2;
		}

	cout<<ans<<endl;

}
	return 0;
}


  1. 站长,你好!
    你创办的的网站非常好,为我们学习算法练习编程提供了一个很好的平台,我想给你提个小建议,就是要能把每道题目的难度标出来就好了,这样我们学习起来会有一个循序渐进的过程!

  2. int half(int *array,int len,int key)
    {
    int l=0,r=len;
    while(l<r)
    {
    int m=(l+r)>>1;
    if(key>array )l=m+1;
    else if(key<array )r=m;
    else return m;
    }
    return -1;
    }
    这种就能避免一些Bug
    l,m,r
    左边是l,m;右边就是m+1,r;