(hdu 簡單題 128道)Lowest Bit(求一個數的二進位的最後一個非零位對應的十進位數),hdulowest
題目:
Lowest BitTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8722 Accepted Submission(s): 6428
Problem DescriptionGiven 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.
InputEach 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.
OutputFor each A in the input, output a line containing only its lowest bit.
Sample Input
26880
Sample Output
28
AuthorSHI, Xiaohan
SourceZhejiang University Local Contest 2005
RecommendIgnatius.L | We have carefully selected several similar problems for you: 1200 1201 1008 1004 1106
題目分析:
簡單題。在將10進位數轉換成2進位數的過程中不斷地判斷當前位是否是非零位即可。
相應的知識點:
在這時候可能需要回顧一下10進位轉2進位的過程:
代碼如下:
/* * d.cpp * * Created on: 2015年3月11日 * Author: Administrator */#include <iostream>#include <cstdio>#include <cmath>using namespace std;int main(){int n;while(scanf("%d",&n)!=EOF,n){int t = 0;while(n%2 == 0){t++;n /= 2;}printf("%d\n",(int)pow(2+0.0,t));}return 0;}