您的位置:首页 > 编程语言 > Java开发

hdu2051 Bitset (十进制转化为二进制) java

2016-10-14 00:37 411 查看
Problem Description

Give you a number on base ten,you should output it on base two.(0 < n < 1000)

Input

For each case there is a postive number n on base ten, end of file.

Output

For each case output a number on base two.

Sample Input

1

2

3

Sample Output

1

10

11

方法一: 直接用Integer中自带方法

import java.util.Scanner;

public class Main {
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n;
while(sc.hasNext()){
n=sc.nextInt();
System.out.println(Integer.toBinaryString(n));
}
}
}


方法二: 利用栈原理

import java.util.*;
public class Main {
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n;
Stack<Integer> stack=new Stack<Integer>();
while(sc.hasNext()){
n=sc.nextInt();
while(n!=0){
int r;
r=n%2;
n=n/2;
stack.push(new Integer(r));
}
while(!stack.isEmpty()){
System.out.print(stack.pop());
}
System.out.println();

}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: