现在的位置: 首页 > 综合 > 正文

九度OJ 1197:奇偶校验 简单的位操作习题

2018年05月25日 ⁄ 综合 ⁄ 共 491字 ⁄ 字号 评论关闭

    华科的这道考研上机题的大意是,给一个字符串,计算每个字符的二进制表示中的1的个数,如果有奇数个1,则奇偶位为0, 偶数个1,则奇偶位为1。

    题目URL:http://ac.jobdu.com/problem.php?id=1197

    我的AC代码:

   

#include <iostream>
#include <stdio.h>
using namespace std;

const int Max = 100 + 10;
char d[Max];

int main()
{
    int pos, ones;

    while(scanf("%s", d) != EOF)
    {
        pos = ones = 0;
        while(d[pos])
        {
            ones = 0;
            for(int i(0); i<7; ++i)
                if(d[pos] & (1 << i)) ++ones;

            if(ones & 1) printf("0");
            else printf("1");
            for(int i(6); i>=0; --i)
                if(d[pos] & (1 << i)) printf("1");
                else printf("0");
            printf("\n");

            ++pos;
        }
    }
    //system("pause");
    return 0;
}


抱歉!评论已关闭.