// 统计一个数二进制中的1的个数 #include <stdio.h> int count(int a) { int count = 0; while (a) { count++; a = a & (a - 1); } return count; } int main() { printf("%d\n", count(10)); printf("%d\n", count(0)); printf("%d\n", count(-
题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 直观思路就是把二进制表示从右往左统计1的个数.直接想到移位操作来迭代处理.坑点在于负数的移位操作会填充1.有人贴出了逻辑移位操作,但还是麻烦.直接按照int的位数,32或64,做这么多次移位操作就好了,每次移位操作累计是否为1.int的位数是32还是64,可以写一个函数来做到,而不是硬编码. class Solution { public: int NumberOf1(int n) { int cnt = 0; int
统计无向图中三角形的个数,复杂度m*sqrt(m). #include<stdio.h> #include<vector> #include<set> #include<math.h> #include<algorithm> using namespace std; #define LL long long vector<]; set<LL> st; ], link[], ]; int main(void) { LL ans,
http://acm.hdu.edu.cn/showproblem.php?pid=5384 Problem Description Danganronpa is a video game franchise created and developed by Spike Chunsoft, the series' name is compounded from the Japanese words for "bullet" (dangan) and "refutation&q
题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 题目代码 /** * 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. * Created by YuKai Fan on 2018/8/28. */ public class countNumberOf1 { /** * 方法一: * *如果一个整数不为0,那么这个整数至少有一位是1.如果我们把这个整数减1, * 那么原来处在整数最右边的1就会变为0,原来在1后面的所有的0都会变成1(如果最右边的1后面