【进制转换】36:二进制分类

36:二进制分类

总时间限制: 
1000ms
内存限制: 
65536kB
描述

若将一个正整数化为二进制数,在此二进制数中,我们将数字1的个数多于数字0的个数的这类二进制数称为A类数,否则就称其为B类数。

例如:

(13)10 = (1101)2,其中1的个数为3,0的个数为1,则称此数为A类数;

(10)10 = (1010)2,其中1的个数为2,0的个数也为2,称此数为B类数;

(24)10 = (11000)2,其中1的个数为2,0的个数为3,则称此数为B类数;

程序要求:求出1~1000之中(包括1与1000),全部A、B两类数的个数。


输入
无。
输出
一行,包含两个整数,分别是A类数和B类数的个数,中间用单个空格隔开。
样例输入
(无)
样例输出
(不提供)
来源
NOIP1995复赛 普及组 第三题




#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int two[30];
int a,b;
int ten2two(int t){
	memset(two,0,sizeof(two));
	int size=0;
	do{
		two[size++]=t%2;
		t/=2;
	}while(t!=0);
	return size;
}
int main(){
	int countA=0,countB=0;
	for(int i=1;i<=1000;i++){
		int count1=0;
		int size=ten2two(i);
		for(int j=0;j<size;j++){
			if(two[j]==1){
				count1++;
			}
		}
		int count0=size-count1;
		if(count1>count0){
			countA++;
		}else{
			countB++;
		}
	}
	printf("%d %d",countA,countB);
}

猜你喜欢

转载自blog.csdn.net/qq_33837704/article/details/80362420