1 solutions

  • 0
    @ 2025-3-3 16:24:08

    C++ :

    #include<bits/stdc++.h>
    using namespace std;
    
    //10进制转2进制 
    int n; 
    string r = "";//存放转换结果 
    int c0 = 0,c1 = 0; 
    int main(){
    	cin>>n;
    	if(n == 0){
    		cout<<1<<" "<<0<<endl;
    		return 0;
    	}
    	
    	//当n!=0循环
    	while(n != 0){
    		//cout<<n%2;//取余 
    		//将n%2的结果转换为字符+到r前面
    		//r = char(n%2+'0') + r; 
    		if(n % 2 == 0) c0++;
    		else c1++;
    		n=n/2;//除2 
    	} 
    	
    	//特判输入为0的情况 
    	cout<<c0<<" "<<c1<<endl;
        return 0;
    } 
    
    • 1

    Information

    ID
    10008
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By