编程之美2.2 求N!中最低位1 的位置

2014-11-23 22:13:28 ? 作者: ? 浏览: 7
/*=============
两种方法求N!的二进制表示中最低位1的位置
2013-09-10 14:33 by Mei
===============*/
#include 
using namespace std;

int num(int n)
{
	int a = 0;
	while(n%2==0)
	{
		a++;
		n = n/2;
	}
	return a;
}

//第一种方法
int first(int n)
{
	int ret = 0;
	for(int i=2; i<=n;i+=2)
	{
		ret += num(i);
	}
	return ret+1;
}

//第二种方法
int second(int n)
{
	int ret = 0;
	while(n)
	{
		n >>= 1;
		ret += n;
	}
	return ret+1;
}
int main()
{
	int n;
	cout <<"请输入n: ";
	cin >> n;
	cout < 
 

-->

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: