top1编程
← 返回题目
题解

【入门】A类多?B类多?

1 条题解

  • 0
    @ 2026-7-30 1:10:04

    解题思路

    统计两类数据各有多少个,再比较两个计数的大小。

    参考代码

    // 读取题目给出的数据。
    // 按照题意完成计算。
    // 这道题的做法:先读入题目给出的数据,再用简单的循环和判断完成要求。
    #include<iostream>
    using namespace std;
    
     
    bool pd(int n);
    
    int main(){
    	
    	int n=0;
    	cin>>n;
    	int a=0,b=0;
    	
    	for(int i=1;i<=n;i++){
    			if(pd(i))
    				a++;
    			else
    				b++;
    	}
    	 
    	cout<<a<<" "<<b;
    
    	
    }
    
    bool pd(int n){
    	int x=n;
    	int y=0;
    	
    	int a=0,b=0;
    	
    	while(x!=0){
    			y=x%2; 
    			x=x/2;
    			
    			if(y==0)
    				b++;
    			else
    				a++;		
    	}
    
    	if(a>b)
    		return true;
    	else
    		return false;	
    	
    }
    

    复杂度

    代码只使用了简单变量、循环和判断。若循环检查了 n 个数据,时间复杂度通常为 O(n);没有开辟与输入规模相关的额外数组时,空间复杂度为 O(1)。

    • 1