华为oj----求int型数据在内存中存储时1的个数



  • 题目背景

输入一个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;
}

猜你喜欢

转载自blog.csdn.net/nameix/article/details/80317832