题解 | #十进制数转二进制数# JAVA31

十进制数转二进制数

http://www.nowcoder.com/practice/90d2de77e05e497eacc85e6b50272900


public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();

        //write your code here......
        StringBuffer sb = new StringBuffer();
        sb.append(num % 2);
        while((num/=2) > 0) {
            sb.insert(0, num%2); // 
        }
        System.out.println(sb);
    }
    
    public static void main2(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();

        //write your code here......
        System.out.println(Integer.toBinaryString(num));
    }
    
    public static void main1(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();

        //write your code here......
        ArrayList<Integer> list = new ArrayList();
        if(num == 0) {
            System.out.println(num);
        }else {
            while (num > 0) {
                list.add(num % 2);
                num /= 2;
            }
            ListIterator<Integer> it = list.listIterator(list.size());
            while (it.hasPrevious()) {
                System.out.print(it.previous());
            }
        }
    }
}
全部评论
666
点赞 回复 分享
发布于 2022-09-11 19:32 四川

相关推荐

小叮当411:应该是1-3个月吧
点赞 评论 收藏
分享
昨天 12:47
门头沟学院 Java
码农索隆:竟然还真有卡体检报告的
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
16
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务