Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
给定一个整数N>;0,区间[0,2^N)中有多少整数正好有N-1个设置位?编写一个短函数,返回正确答案 我编写了一些C++代码来解决这个问题: #include <iostream> #include <cmath> using namespace std; unsigned int countSetBits(unsigned int n) { int totalCount = 0, i; int cond = n-1; for(i=0;i<pow(2,n);i++){ unsigned int count = 0; while (i) { count += i & 1; i >>= 1; } if(count == cond){ totalCount++; } } return totalCount; } int main() { int n=5; cout<<countSetBits(5); return 0; } #包括 #包括 使用名称空间std; 无符号整数计数位(无符号整数n) { int totalCount=0,i; int cond=n-1; 对于(i=0;i>=1; } 如果(计数==秒){ totalCount++; } } 返回totalCount; } int main() { int n=5; cout_C++_Algorithm_Integer_Byte - Fatal编程技术网

给定一个整数N>;0,区间[0,2^N)中有多少整数正好有N-1个设置位?编写一个短函数,返回正确答案 我编写了一些C++代码来解决这个问题: #include <iostream> #include <cmath> using namespace std; unsigned int countSetBits(unsigned int n) { int totalCount = 0, i; int cond = n-1; for(i=0;i<pow(2,n);i++){ unsigned int count = 0; while (i) { count += i & 1; i >>= 1; } if(count == cond){ totalCount++; } } return totalCount; } int main() { int n=5; cout<<countSetBits(5); return 0; } #包括 #包括 使用名称空间std; 无符号整数计数位(无符号整数n) { int totalCount=0,i; int cond=n-1; 对于(i=0;i>=1; } 如果(计数==秒){ totalCount++; } } 返回totalCount; } int main() { int n=5; cout

给定一个整数N>;0,区间[0,2^N)中有多少整数正好有N-1个设置位?编写一个短函数,返回正确答案 我编写了一些C++代码来解决这个问题: #include <iostream> #include <cmath> using namespace std; unsigned int countSetBits(unsigned int n) { int totalCount = 0, i; int cond = n-1; for(i=0;i<pow(2,n);i++){ unsigned int count = 0; while (i) { count += i & 1; i >>= 1; } if(count == cond){ totalCount++; } } return totalCount; } int main() { int n=5; cout<<countSetBits(5); return 0; } #包括 #包括 使用名称空间std; 无符号整数计数位(无符号整数n) { int totalCount=0,i; int cond=n-1; 对于(i=0;i>=1; } 如果(计数==秒){ totalCount++; } } 返回totalCount; } int main() { int n=5; cout,c++,algorithm,integer,byte,C++,Algorithm,Integer,Byte,问题在于,您正在修改该循环中for循环的“控制”变量(i),因此i的值将永远不会达到循环限制,它将永远运行 要解决此问题,请复制当前i值并修改: unsigned int countsetbit(unsigned int n) { int totalCount=0,i; int cond=n-1; int limit=pow(2,n);///计算一次,而不是在每个循环上! 对于(i=0;i>=1; } 如果(计数==秒){ totalCount++; } } 返回totalCount; } 如

问题在于,您正在修改该循环中
for
循环的“控制”变量(
i
),因此
i
的值将永远不会达到循环限制,它将永远运行

要解决此问题,请复制当前
i
值并修改:

unsigned int countsetbit(unsigned int n)
{
int totalCount=0,i;
int cond=n-1;
int limit=pow(2,n);///计算一次,而不是在每个循环上!
对于(i=0;i>=1;
}
如果(计数==秒){
totalCount++;
}
}
返回totalCount;
}

如评论中所述,您还可以进行其他改进。但我发布的代码更改至少解决了您的问题。

纸笔解决方案:

N=2  2^N-1 = 0b11    Possible integers with 1 bit set:
01
10

因此,
N
似乎是答案。

您在循环内修改
i
。内部while循环将始终使
i==0
,无论循环计数器是什么,因此for循环的条件永远不会为假。使用临时值。也不要使用
pow
来计算
2
的幂de>是<代码>(1<代码>功率(2,n)--不要对整数指数使用
pow
。另外,当您使用调试器时,您何时检测到
while
循环终止?听起来您可能需要学习如何使用调试器来逐步执行代码。有了一个好的调试器,您可以逐行执行程序,并查看它偏离了什么如果你要做任何编程,这是一个必不可少的工具。进一步阅读:延伸Paul的评论,当你使用二的指数时,使用左移位运算符(我认为你不应该使用蛮力。在
(2^N)-1
中设置了多少位?搜索互联网上的“位旋转”对于一些算法帮助。回答很好,Ted-但我认为OP应该编写一个计算答案的函数-尽管:
unsigned int countSetBits(unsigned int n){return n;}
给出了正确的答案!!也许吧,但是“编写一个返回正确答案的短函数”并没有这么多说明:-)这是一个很好的答案。如果您提到N是唯一可以放置0的位位置数,这将是一个很好的答案(使用二项式系数对
k
1s进行推广).@rici是一个你期望任何人做我演示过的纸笔练习都会得出的结论吗?第一部分在我的答案中,对吗?@ted:我希望他们没有纸笔也能得出这个结论,但是纸笔列表的对角线非常明显。所以,是的。
N=3  2^N-1 = 0b111   Possible integers with 2 bits set:
011
101
110
N=4  2^N-1 = 0b1111  Possible integers with 3 bits set:
0111
1011
1101
1110
unsigned int countSetBits(unsigned int n)  { return n; }