本文共 785 字,大约阅读时间需要 2 分钟。
代码如下:
#include#include #include using namespace std; int main(){ int n = 10002; int num = 0; //1的个数 int tmp = n; vector pos; //1的位置 while (n) { n &= (n-1); num++; pos.push_back(log2(tmp-n) + 1); tmp = n; } cout << "the number of 1 is " << num << endl; cout << "the position is " << endl; for (vector ::iterator i = pos.begin(); i != pos.end(); i++) { cout << *i << endl; }}
输出结果:
the number of 1 is 6 the position is 2 5 9 10 11 14转载地址:http://iyxyx.baihongyu.com/