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; }