一道c语言编程题 在线等!

2024-12-26 18:59:03
推荐回答(1个)
回答1:

#include
#include

void NumberOf1and0_Solution(int i,int* count0,int* count1)
{
*count0 = 0;
*count1 = 0;

while(i)
{
if(i & 1)
*count1+=1;
else *count0+=1;

i = i >> 1;
}
}

void main()
{
int N,countA=0,countB=0;
int nZero,nOne,i;

for(i=1;i<=1000;i++)
{
NumberOf1and0_Solution(i,&nZero,&nOne);

if(nOne>nZero) countA++;
else countB++;
}

printf( "1~1000之间,A类数有%d个,B类数有%d个\n",countA,countB);
system( "pause ");
}