Parity UVA - 10931

AC代码(就是把一个数转化为二进制然后看他有几个1)

Select Code

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main()
{
    std::ios::sync_with_stdio(false);
    long long n;
    int a[100], i;
    while(cin>>n&&n!=0)
    {
       long long int h = 0, sm = 0;
        while(n)
        {
            a[h++] = n%2;
            if(n%2==1)
            sm++;
            n = n/2;
        }
        printf("The parity of ");
        for(i = h-1;i>=0;i--)
        {
         printf("%d",a[i]);
        }
        printf(" is %lld (mod 2).\n",sm);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41524782/article/details/81782228
今日推荐