打印N的二进制表示(algs4)

来源:互联网 发布:php截取字符串第一位 编辑:程序博客网 时间:2024/06/05 01:00

栈的应用

除二取余,逆序排列

package _1_3linkedList;import java.util.Scanner;/*打印N的二进制表示*/public class BinaryRepresentation{public static void main (String[] args) {Stack<Integer> stack=new Stack<Integer>();Scanner sc=new Scanner(System.in);Integer n=sc.nextInt();                    /*n也可为int类型*/while(n>0){stack.push(n%2);n=n/2;}for(int i:stack)System.out.print(i);sc.close();}}


33100001