码迷,mamicode.com
首页 > 其他好文 > 详细

二进制中的1

时间:2017-12-16 21:13:15      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:str   java   system.in   static   span   next   --   otto   nbsp   

题目描述

给定一个正整数N,请找出N转化为二进制后,其中所有1的位置。二进制的最低位(最右侧)为第0位。
 

输入

输入中包含一个正整数N ( 1 <= N <= 1000000)
 

输出

输出N转换为二进制后,所有1的位置,每行一个1的位置。
 

样例输入 [复制]

13

样例输出 [复制]

0 2 3
 
AC代码:
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String str = Integer.toBinaryString(n);
        for (int i = str.length() - 1; i >= 0; i--) {
            if (str.charAt(i) == ‘1‘)
                System.out.println(str.length() - i - 1);
        }
    }
}

 

二进制中的1

标签:str   java   system.in   static   span   next   --   otto   nbsp   

原文地址:http://www.cnblogs.com/ixummer/p/8047606.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!