9度_タイトル1513:バイナリの中の1の個数

1223 ワード

タイトルの説明:
整数を入力し、その数のバイナリ表現の1つの数を出力します.ここで負数は補数で表される.
入力:
入力には、複数のテストサンプルが含まれる場合があります.
各入力ファイルについて、最初の行には、テストサンプルの数を表す整数Tが入力されます.テストサンプルごとに整数として入力します.
.nはint範囲内の整数であることを保証する.
出力:
各テストケースに対応し、
入力した数のうち1の数を表す整数を出力します.
サンプル入力:
3
4
5
-1
サンプル出力:
1
2
32
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
int main()
{
    int count=0;
    cin>>count;//the number of test cases
    long long  num;//the input num
    int theNum=0;//the the Num to count the number of 1
    long long max=(long long)pow(2,32);//to change the negative number
    for(int i=0;i<count;i++)
    {
        scanf("%lld",&num);
        if(num<0)num+=max;
        while(num)
        {
            if(num%2)theNum++;
            num/=2;
             
        }
        printf("%d
",theNum); theNum=0; } return 0; } /************************************************************** Problem: 1513 User: hndxztf Language: C++ Result: Accepted Time:100 ms Memory:1520 kb ****************************************************************/