您的位置:首页 > 其它

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

2012-02-15 09:19 253 查看
华科的这道考研上机题的大意是,给一个字符串,计算每个字符的二进制表示中的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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: