首页
DreamJudge
院校信息
考研初试
机试真题
讨论区
兑换中心
登录
注册
上岸
以下题解仅供学习参考使用。
抄袭、复制题解,以达到刷AC率/AC数量或其他目的的行为,在N诺是严格禁止的。
N诺非常重视学术诚信。此类行为将会导致您成为作弊者。具体细则请查看N诺社区规则。
woshipzg123
2021年2月16日 10:04
c++这个题怎么也通不过,原来是要数32位,一直是从1开始数
P1008
回复 0
|
赞 0
|
浏览 8.5k
#include <iostream> #include <algorithm> using namespace std; int main() { int n,count0=0,count1=0; cin>>n; for(int i=0;i<31;++i) { if(n%2==1) count1++; n=n/2; if(n==0) break; } cout<<"count0=&...
swing123
2021年2月15日 17:37
不造为啥编译没过,明明在编译器里面过了
P1008
回复 0
|
赞 0
|
浏览 8.3k
#include<stdio.h> #include<stdlib.h> int main() { int n,count1=0,i; char s[1000]; scanf("%d",&n); itoa(n,s,2); for(i=0;s[i]!='\0';i++) { &n...
烟杨绿未成
2020年5月8日 10:36
此处int占4字节,即32位。求出1的个数后,用32减去即为0的个数
P1008
回复 0
|
赞 0
|
浏览 11.0k
#include<iostream> #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> #include<map> using namespace std; int main() { int n, cnt = 0; char s[100]; int count1 = 0; ...
Trisirt
2020年4月13日 18:07
#1008 0和1的个数,本地能通过,但oj上编译错误
P1008
回复 1
|
赞 0
|
浏览 10.9k
#include #include int main() { int n; char bin[33]; scanf("%d", &n); int cnt0 = 0, cnt1 = 0; itoa(n, bin, 2); //感觉是这个itoa()编译不通过 for(int i = 0; i < 32; i++) { if(bin[i] == '1') cnt1++; else cnt0++; } ...
莫小七
2020年2月21日 13:40
1008二进制0和1的个数(不要忽略掉int的问题!!!!)
P1008
回复 0
|
赞 9
|
浏览 15.7k
#include #include using namespace std; int main() { bitset<32>bit;//bitset中只有“0”,“1”两个元素,且每个元素只占1bit空间 int n; while(cin >> n) { bit = n; bit = bit.flip(); cout << "count0=" << bi...
创世的背影
2019年12月4日 20:22
0和1的个数(感觉题目有点问题,可能是我没看懂)
P1008
回复 1
|
赞 0
|
浏览 10.2k
void main() { int a,b[81],i,x,y; scanf("%d",&a); for(i=0;a>=1;i++) { b[i]=(a%2); a=(a-b[i])/2; ...
mzymzyo
2020年2月29日 23:59
题解:0和1的个数
P1008
回复 1
|
赞 3
|
浏览 12.3k
分享个函数,__builtin_popcount(),返回int类型的数二进制下1的个数 #include<cstdio> #include<iostream> using namespace std; int main() { int n; cin >> n; int ans = __builtin_popcount(n); printf("count0=%d count1=%d", 32 - ans, ans); return 0; }
myhy001
2019年12月8日 18:54
int型4字节一个字节存放8个0或者1
P1008
回复 0
|
赞 3
|
浏览 9.6k
#include<stdio.h> int main() { int n,i=0,j=0; scanf("%d",&n); while(n>=1) { if(n%2==1) j++; n=n/2;...
南山不落梅
2019年12月8日 22:01
大家来评评理,我这个能算出结果但是提交答案错误
P1008
回复 2
|
赞 0
|
浏览 8.9k
#include<stdio.h> int main(){ int a,one=0; scanf("%d",&a); for(;a!=0;a=a/2,one+=1); printf("count0=%d count1=%d",32-one,one); return 0; }
codesuc
2020年3月5日 17:15
虽然这里通过了,但编译器没出结果,搞求不懂
P1008
回复 2
|
赞 0
|
浏览 10.1k
#include<stdio.h> int main(){ int i=0,j=0,a,b; scanf("%d",&a); while(a!=0){ b=a%2; a=a/2; if(b==1) i+=1; } j=32-i; printf("count0=%d count1=%d",j,i); return 0; }
1
2
3
4
题目
0和1的个数
题解数量
34
发布题解
在线答疑
热门题解
1
0和1的个数 题解:wihle循环实现除留余数法,将十进制转换成二进制
2
1008二进制0和1的个数(不要忽略掉int的问题!!!!)
3
0和1的个数 题解:c解决 送分题
4
C++常规解法
5
0和1的个数 (位运算)题解:
6
0和1的个数 题解:超简单思路
7
0和1的个数 题解:
8
不断除以2,看最后一位的奇偶
9
0和1的个数 题解:代码相对来说简洁
10
C++