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

相关文章
|
Java 人工智能
|
Java BI
HDU 2034 人见人爱A-B
人见人爱A-B Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 77157    Accepted Submission(s): 21509 Problem Description 参加过上个月月赛的同学一定还记得其中的一个最简单的题目,就是{A}+{B},那个题目求的是两个集合的并集,今天我们这个A-B求的是两个集合的差,就是做集合的减法运算。
1163 0
|
机器学习/深度学习
hdu1059Dividing
题意:有6种石头,价值分别是1,2,3,4,5,6   每种有若干,作为输入数据。问能否把这些石头按照价值均分? 分析:多重背包问题。 代码: View Code 1 #include 2 #include 3 #include 4 using namespace...
874 0