首页 > 试题广场 >

奇偶校验

[编程题]奇偶校验
  • 热度指数:10117 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
输入一个字符串,然后对每个字符进行奇校验,最后输出校验后的二进制数(如'3’,输出:10110011)。

输入描述:
输入包括一个字符串,字符串长度不超过100。


输出描述:
可能有多组测试数据,对于每组数据,
对于字符串中的每一个字符,输出按题目进行奇偶校验后的数,每个字符校验的结果占一行。
示例1

输入

3
3a

输出

10110011
10110011
01100001

python3解法:

这个题就是找输入的字符的8位(ascii=>bin)表示中1的个数,如果1的个数为偶数,就把最高位置1.


while True:
    try:
        string=input()
        for i in string:
            res=bin(ord(i)).replace("0b","").rjust(8,"0")
            print("1"+res[1:] if res.count("1")%2==0 else res)

    except:
        break
发表于 2017-10-17 09:05:47 回复(0)
while True:
    try:
        for c in raw_input():
            s = '{:07b}'.format(ord(c))
            print '1' + s if s.count('1') & 1 == 0 else '0' + s
    except EOFError:
        break

编辑于 2017-01-06 21:31:55 回复(0)

问题信息

难度:
3条回答 12069浏览

热门推荐

通过挑战的用户

查看代码