HDU1169-Lowest Bit

简介:
Lowest Bit
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 7514    Accepted Submission(s): 5517


Problem Description
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.

Input
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.

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

Sample Input
26
88
0
 
Sample Output
2
8
 

水题,为了测试强大的itoa函数
itoa函数简介:

功能:将任意类型的数字转换为字符串。在<stdlib.h>中与之有相反功能的函数是atoi。
用法

char *itoa(int value, char *string, int radix);
int value 被转换的整数,char *string 转换后储存的字符数组,int radix 转换进制数,如2,8,10,16 进制等
头文件: <stdlib.h>

比如十进制数n转二进制,可以写itoa(n,a,2)(转换成一个字符串形式的数,a是n转换成的字符型数)


AC代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h> 
char s[1000];
int main()
{
    int i,j,n,m,sum,p;
    while(scanf("%d",&n)&&n)
    {
       itoa(n,s,2);
       //printf("%s\n",s);
       m=strlen(s);p=0;
       for(i=m-1;i>=0;i--)
       {
          p++;
          if(s[i]=='1')
          {
             m=p-1;
             break;
          }
       }


       sum=1;
       for(i=m-1;i>=0;i--)
       sum*=2;
       printf("%d\n",sum);
    }
    return 0;
}

相关文章
|
6月前
|
Java
hdu-1016-Prime Ring Problem
hdu-1016-Prime Ring Problem
27 0
|
12月前
hdu 1196 Lowest Bit(水题)
hdu 1196 Lowest Bit(水题)
38 0
codeforces 339 D.Xenia and Bit Operations(线段树)
输入n,m表示有2^n个数和m个更新,每次更新只把p位置的值改成b,然后输出整个序列运算后的值,而这个运算就比较复杂了, 最下面一层两个数字之间或运算得到原来数目一半的数字,然后两个之间异或运算,得到一半,再或再异或………………,一直到得到一个数字,这个数字就是要求的结果。
51 0
HDOJ 1196 Lowest Bit(二进制相关的简单题)
HDOJ 1196 Lowest Bit(二进制相关的简单题)
97 0
POJ 2840 Big Clock
POJ 2840 Big Clock
111 0
LeetCode---Problem6 ZigZag Conversion
ZigZag问题思路。代码整洁并不一定执行速度就好~
789 0