zoukankan      html  css  js  c++  java
  • 奇偶校验

    题目截图:

     

    思路:

      先求出字符的 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 }
  • 相关阅读:
    python设置环境变量(临时和永久)
    python items和setdefault函数
    Django学习day3——Django的简单使用
    DOS打印目录树到文件
    Django学习day2——Django安装与环境配置
    Django学习day1——Django的简单介绍
    sets,relations,and fuctions
    lecture 1
    number theory
    lecture 9.18
  • 原文地址:https://www.cnblogs.com/coderJiebao/p/HustTest10.html
Copyright © 2011-2022 走看看