南阳oj 1的个数
#include<iostream>
#include<math.h>
using namespace std;
int main ()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int count=0;
while(n!=0)
{
if(n%2==1)
count++;
n=n/2;
}
cout<<count<<endl;
}
return 0;
}
二進制的求法,n對2求余為二進制的最低位,每次n變為原來的一半,直到為零。
轉載于:https://www.cnblogs.com/huanya/p/9366022.html
總結
- 上一篇: 第五章Bookstrap
- 下一篇: Linux流量监控工具 - iftop