Lowest Bit
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB
Total submit users: 22, Accepted users: 22
Problem 10021 : No special judgement
Problem description
Given an positive integer A (1 <= A <= 10^9), 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 <= 109). 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
8
0
Sample Output
2
8
import java.util.Scanner;
public class Acm10021 {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
Integer a = cin.nextInt();
String binaryNum = null;
String suffixNum = null;
int result;
while(a != 0){
result = 0;
binaryNum = new String();
binaryNum = Integer.toBinaryString(a);
suffixNum = new String();
suffixNum = binaryNum.substring(binaryNum.lastIndexOf("1"), binaryNum.length());
int j=0;
for(int i=suffixNum.length(); i>0; i--){
result += Integer.parseInt(suffixNum.substring(i-1, i))*Math.pow(2, j);
j++;
}
System.out.println(result);
a = cin.nextInt();
}
}
}
分享到:
相关推荐
编译原理OJ-语义分析及目标代码生成c++源码.zip编译原理OJ-语义分析及目标代码生成c++源码.zip编译原理OJ-语义分析及目标代码生成c++源码.zip编译原理OJ-语义分析及目标代码生成c++源码.zip编译原理OJ-语义分析及...
这是九度OJ-题目1509:树中两个结点的最低公共祖先的测试数据,input.txt是输入数据,output.txt是输出数据。
湖南师范大学oj部分ac代码,来自2016级老学长,嘤嘤嘤
sduoj-sandbox.zip
hustoj -- 流行的OJ系统,跨平台、易安装、有题库
a2oj-源码.rar
1_OJ-1163.rar
判断输入字符串是否为镜像或回文串。 来源于UVaOJ - 401. 水题。
湖南大学ACM-OJ的部分题目代码,对学习数据结构和算法很有帮助
官方离线安装包,亲测可用
一个下雪的js特效,挺漂亮的,适用于圣诞节,春节等
HUSTOJ 是采用GPL的自由软件。
这是一个简单的入门ACM问题...用C语言写的,供初学者参考!
leetcode 和 oj 我的 OJ 解决方案 这个存储库包含了我的问题解决方案,来自 ,和望岛书。 目前无法访问。 使用 GCC 7.2.0。
本人搜集的资源,经本人亲测,可用性强!!!适合大家参考...
leetcode 和 oj OJ-Journey 一天一个OJ,医生和老师远离~ 我个人的 OJ 解决方案主要在 SJTUOJ 和 Leetcode。 POJ 可能不会永远刷新。
为了获知基因序列在功能和结构上的相似性,经常需要将几条不同序列的DNA进行比对,以判断该比对的DNA是否具有相关性。 现比对两条长度相同的DNA序列。首先定义两条DNA序列相同位置的碱基为一个碱基对,如果一个碱基...
leetcode 和 oj oj-leetcode-解决方案 我提交的 leetcode,用 C++ 编写并通过 gtest 测试。
课程资源 杭电OJ1000-1099答案 ,仅供参考...
嵌入式Linux开发