数据结构的使用:使用一个栈,将十进制转换为二进制 (使用java语言编写)

2024-11-19 14:53:48
推荐回答(2个)
回答1:

x

回答2:

import java.util.Scanner;
import java.util.Stack;

public class TestNumTran {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入需要转换的数字:");
int num = scan.nextInt();
int ocNum = num;
Stack stack = new Stack();
int flag = 0;
while(num != 0) {
flag = num%2;
if(flag == 0 ) {
stack.push(0);
} else {
stack.push(1);
}
num = num/2;
}
System.out.print(ocNum + "转换成二进制为:");
while(!stack.empty()) {
System.out.print(stack.peek());
stack.pop();
}
}
}

书的下载地址:http://down.down3721.com/book/Javasjjgderban.rar