学习C++的感受
突然觉得C++很不错!题目:求一个数转换为2进制中1的个数
程序代码:
#include <stdio.h>
int cal(int m);
int main()
{
int n;
scanf("%d",&n);
cout<<cal(n)<<endl;
return 0;
}
int cal(int m)
{
int c=0;
while(m)
{
if(m%2) c++;
m/=2;
}
return c;
}
程序代码:
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
unsigned int n;
cin>>n;
bitset<32> b(n);
cout<<b.count()<<endl;
return 0;
}









