HDU1169-Lowest Bit
Lowest BitTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7514 Accepted Submission(s): 5517
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; }
最後更新:2017-04-03 08:26:14