计算一个数二进制中1的个数
2013-01-07 23:59
274 查看
Write a recursive method that returns the number of 1’s in the binary representation of N. Use the fact that is equal to the number of 1’s in therepresentation of N/2, plus 1, if N is odd.
根据要求可以知道题目是说的>=0的数,不然不符合恩
然后上代码
根据要求可以知道题目是说的>=0的数,不然不符合恩
然后上代码
public class NumOfOne { public static int calNum(int n){ if (n==1||n==2) { return 1; }else if (n==0) { return 0; }else{ if (n%2==0) { return calNum(n/2); }else{ return calNum(n-1)+1; } } } public static void main(String[]args){ System.out.println(calNum(7)); } }
相关文章推荐
- 计算一个数字二进制1的个数
- 计算一个数的二进制中1的个数
- 计算一个数的二进制中1的个数
- 编程练习:计算一个数的二进制形式含有几个1
- 计算1个数--计算一个整数二进制位中1的个数。要求效率尽可能的高。且能正确求正数和负数的二进制中1的个数。
- 计算一个十进制数的二进制表示有多少位1
- 【C语言】计算一个数二进制中一的位数
- JS 二进制计算,如何得到一个负数的二进制表示
- 计算一个整数二进制表示时有多少位是为1的算法
- 计算一个字符的二进制表示中有几个1
- 用递归算法计算一个自然数的二进制表示中的“1”的个数
- 算法的强大——快速计算一个正二进制整数中包含多少个1
- [c]位运算计算一个数的二进制
- Fast bit count问题(即计算一个unsigned int的二进制表达中1的数目)
- 计算一个数的二进制的存在的一的位数
- Fast bit count问题(即计算一个unsigned int的二进制表达中1的数目)
- Bit Manipulation-计算一个整数中二进制中1的个数
- 计算一个数的二进制形式有多少个1
- 算法的强大——快速计算一个正二进制整数中包含多少个1
- 计算一个整数的二进制中1的个数