//3、给定一个int型整数,输出这个整数的二进制的0和1的个数。
//样例输入:
//8
//样例输出:
//count0=28 count1=4
#include <iostream>
#include<math.h>
using namespace std;

int main()
{
    int t;
    cin>>t;
    int y=t;
    int b;
    int x[50]={0};
    int count=0;

    int count1=0;
    int count0=0;//记录这个数二进制总位数,以及为1,0的个数。
    while(t>0)
    {
        b=t%2;
        if(b==0) count0++,x[count++]=0;
        if(b==1) count1++,x[count++]=1;//当然此段是可以精简的。
        t=t/2;
    }

    cout<<"the number:"<<y<< " de 2 jinzhi is:";
    for(int i=count-1;i>=0;i--)
        cout<<x[i];

    cout<<endl<<"count0="<<count0<<"  "<<"count1="<<count1<<endl;



    return 0;



}

2018.3给定一个int型整数,输出这个整数的二进制的0和1的个数

相关文章:

  • 2021-10-08
  • 2018-10-21
  • 2021-09-27
  • 2021-10-08
  • 2021-10-18
  • 2020-04-02
  • 2021-09-07
  • 2021-09-17
猜你喜欢
  • 2020-05-31
  • 2021-10-12
  • 2020-06-23
  • 2021-09-07
  • 2021-09-07
  • 2021-10-08
  • 2021-10-08
  • 2019-06-20
相关资源
相似解决方案