• 奇偶校验


    题目截图:

     

    思路:

      先求出字符的 ASCII 码,然后将 ASCII 码转换成七位二进制数,最后根据二进制数 1 的个数进行奇偶校验。

      

    代码如下:

     1 /*
     2     奇偶校验 
     3 */
     4 
     5 #include <stdio.h>
     6 #include <string.h>
     7 #include <math.h>
     8 #include <stdlib.h>
     9 #include <time.h>
    10 #include <stdbool.h>
    11 
    12 int asc[8] = {0};                // 存储 ascii 码的二进制数 
    13 // 将 ascii 转换成相应的二进制数 
    14 void trans(int ascii) {
    15     int i, k=0;
    16     for(i=0; i<8; ++i) {        // 初始化 
    17         asc[i] = 0;
    18     }
    19     while(ascii != 0) {            // 将 ascii 转换成二进制数,倒置 
    20         asc[k++] = ascii%2;
    21         ascii/=2;
    22     }
    23 }
    24 
    25 int main() {
    26     char str[101];
    27     while(scanf("%s", str) != EOF) {
    28         int i, j, ans=0;
    29         for(i=0; i<strlen(str); ++i) {        // 遍历每个字符 
    30             int ascii = str[i]-'';        // 计算 ascii 码,十进制 
    31             trans(ascii);                    // 转换成二进制 
    32             ans = 0;                        // ans 储存二进制中 1 的个数 
    33             for(j=0; j<8; ++j) {
    34                 ans += asc[j];
    35             }
    36             if(!(ans%2)) {                    // 奇校验 
    37                 asc[7] = 1;
    38             }
    39             for(j=7; j>=0; --j) {            // 按要求输出 
    40                 printf("%d", asc[j]);
    41             }
    42             printf("
    ");
    43         }
    44     } 
    45 
    46     return 0;
    47 }
  • 相关阅读:
    mysql-centos7部署mysql5.7.20
    tips-history添加命令的执行时间
    Maven核心概述
    Java动态代理
    JDBC(3)-数据库事务
    JDBC(2)-操作BLOB类型字段和批量插入
    redis+mysql读写方案
    Redis与Memcache区别最全整理【摘自百度】
    redis+mysql有几种用法?【摘录知乎】
    js 拷贝到剪贴板
  • 原文地址:https://www.cnblogs.com/coderJiebao/p/HustTest10.html
Copyright © 2020-2023  润新知