java: recursion binary representation of the number of bits

java: recursion binary representation of the number of bits

topic

问题描述
  给定一个十进制整数,返回其对应的二进制数的位数。例如,输入十进制数9,其对应的二进制数是1001,因此位数是4。
样例输入
一个满足题目要求的输入范例。
9
样例输出
与上面的样例输入对应的输出。

数据规模和约定
  输入数据中每一个数的范围。
  例:输入在int表示范围内。
import java.util.Scanner;

public class 递归求二进制表示位数 {
	public static void main(String[] args){
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int cnt=0;
		while(n!=0){
			cnt++;
			n/=2;
		}
		System.out.println(cnt);
	}
}

Published 146 original articles · won praise 3 · Views 2788

Guess you like

Origin blog.csdn.net/weixin_44522477/article/details/104557675