输入一个int型数据,计算出该int型数据在内存中存储时1的个数。
Int GetCountOfOne(Int nNum);
不允许使用位操作符,包含位移,位与,或等。
例如输入的数据是5,返回2,输入-5,返回31。 |
#include <stdlib.h> #include <string.h> #include "oj.h" /* 功能: 输入:整型 输出: 返回:返回1的个数 */ int GetCount(int iValue) { int count = 0; while (iValue) { iValue &= (iValue-1); count++; } return count; }