Time limit: 3.000 seconds
限时:3.000秒

Problem
问题

You work for a company which uses lots of personal computers. Your boss, Dr Penny Pincher, has wanted to link the computers together for some time but has been unwilling to spend any money on the Ethernet boards you have recommended. You, unwittingly, have pointed out that each of the PCs has come from the vendor with an asynchronous serial port at no extra cost. Dr Pincher, of course, recognizes her opportunity and assigns you the task of writing the software necessary to allow communication between PCs.
你在一家有很多PC机的公司工作。你的老板是Penny Pincher博士,她想把所有电脑都连接起来。你提出的建议是购买网卡,但她觉得太贵了。此时,你无意间想到这些计算机都来自同一个供货商,并且都有一个异步通信的串口可以直接使用而无需花钱。Pincher博士采纳了这个主意,并让你编写用于连通计算机(通过串口的异步通信方式)的软件。

You've read a bit about communications and know that every transmission is subject to error and that the typical solution to this problem is to append some error checking information to the end of each message. This information allows the receiving program to detect when a transmission error has occurred (in most cases). So, off you go to the library, borrow the biggest book on communications you can find and spend your weekend (unpaid overtime) reading about error checking.
后来,你在某次通信过程中读取了一些数据位,结果发现传输中出现了错误。解决这个问题的传统办法就是在每条报文的后面都附加一段错误校验信息。这样软件就可以利用这些校验信息来检测传输过程中是否出现了错误(在绝大多数情况下都有效)。在此后的一周里你天天去泡图书馆(在没有加班费的空闲时间),借了最厚的通信书籍查阅有关错误校验的知识。

Finally you decide that CRC (cyclic redundancy check) is the best error checking for your situation and write a note to Dr Pincher detailing the proposed error checking mechanism noted below.
最后你确定使用CRC(循环冗余校验)是解决当前问题的最佳方案,并给Pincher博士递交了一分关于错误校验机制的说明,如下:

CRC Generation
CRC的生成

The message to be transmitted is viewed as a long positive binary number. The first byte of the message is treated as the most significant byte of the binary number. The second byte is the next most significant, etc. This binary number will be called "m" (for message). Instead of transmitting "m" you will transmit a message, "m2", consisting of "m" followed by a two-byte CRC value.
传输的报文可以视为一个很长的二进制数。报文的1个字节是整个二进制数的最高位,第2个字节其次,等等。将此二进制数称为“m”(message, 报文),在传输m时要附加两个字节的CRC编码,称为“m2”。

The CRC value is chosen so that "m2" when divided by a certain 16-bit value "g" leaves a remainder of 0. This makes it easy for the receiving program to determine whether the message has been corrupted by transmission errors. It simply divides any message received by "g". If the remainder of the division is zero, it is assumed that no error has occurred.
选择的CRC码应该能使m2能被一个16位的数“g”整除,这样的话接收方软件就可以非常容易的确定消息在传输过程中是否出错。只需要将收到的每一条消息除以“g”,如果余数为0就表示没有错误发生。

You notice that most of the suggested values of "g" in the book are odd, but don't see any other similarities, so you select the value 34943 for "g" (the generator value).
你除了注意到书中建议的大多数“g”值都是奇数外没发现其它的规律,因此你选择“34943”作为“g”的值。

Input and Output
输入与输出

You are to devise an algorithm for calculating the CRC value corresponding to any message that might be sent. To test this algorithm you will write a program which reads lines (each line being all characters up to, but not including the end of line character) as input, and for each line calculates the CRC value for the message contained in the line, and writes the numeric value of the CRC bytes (in hexadecimal notation) on an output line. Each input line will contain no more than 1024 ASCII characters. The input is terminated by a line that contains a # in column 1. Note that each CRC printed should be in the range 0 to 34942 (decimal).
你要设计一种算法,为要发送的所有 报文计算出CRC码。为了测试你的算法,你还要写一个程序来读取每行输入的字串(每行都从开始一直到(但不包括)换行符结束),并为每一行字串表示的消息计算出CRC值,然后对应的输出一行,打印出CRC字节的数值。每行输入都不会超过1024个ASCII字符。首字符为#号的一行表示输入结束。注意每个CRC都应该在0到34942(十进制)的范围内。

Sample Input
输入示例

this is a test

A
#

Sample Output
输出示例

77 FD
00 00
0C 86

Analysis
分析

实际在通迅中使用的CRC校验码要比题目中描述的复杂得多,涉及的知识点也很广,这里仅介绍与题目相关的算法,了解其它内容请参见“循环冗余校验”(维基百科)。

本题最主要的算法就是将字符串看作一个很长的整数,然后执行除法取余。简单来讲,就是要实现大数的除法。我们先来看10进制的例子,请用笔计算:2357 ÷ 6。我们只关心余数,第一次在3上面试商3,下面得余数5;用5 × 10 + 5 = 55作为第二次被除数,商9得余数1;用1 × 10 + 7 = 17作第三次被除数,商2余5。注意到每次被除数的每一位去除以除数6得到的余数都要累计到下一位(乘以10再加下一位)。只要能保证被除数中每一位都大于除数,就可以避免出现借位的情况。将笔算除法推广到n进制,即得到大数除法。设p为一个n进制整数作为被除数,q为小于n的除数(即选择的校验码生成数):

p = a0n0 + a1n1 + ... + aknk

现在要求的是p ÷ q的余数rk,即r0 = p mod q,先从rk开始向前计算:

rk = aknk mod q
rk-1 = (rkn + ak-1nk-1) mod q
...
r0 = (r1n + a0n0) mod q

根据此递推公式就很容易处理大数的除法了。为了方便实现,在程序中可以使用216=65535进制或232=4294967295进制。这样在计算余数时,无需将上一个r乘以进制,只要左移16位或32位即可,最大限度的利用了寄存器和CPU的特性。

求得余数后,下一步就是要将余数转为校验码。设校验码为c,那么应满足:

(a0n0 + a1n1 + ... + aknk + c) mod q = 0

前面已求得:

(a0n0 + a1n1 + ... + aknk) mod q = r

显然,c的值不止一个,但最小的正值c可用如下公式来计算:

c = q - (nr mod q)

上式很容易理解,就不赘述了。至此,CRC码计算完成。这里还有一个要注意的地方,我们一般写程序和编译的机器环境(包括OJ系统运行的环境)都是x86架构的,也就意味着字节序是little-endian, 即在存储器中的所有整型数值都是高位在前低位在后。比如32位16进制数:AF045Bh,在内存中的顺序应该是:

5B 04 AF 00

如果我们直接将字符串的指针转为int型指针进行计算就会算错。必须先对每一组字节(整型变量大小的一组)先进行反转再作运算。

Solution
解答

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
int main(void) {typedef unsigned short word;char Bits[1032]; //存储输入的字符串cin.sync_with_stdio(false); //输入的数据量很大,关闭同步以加速cout << setbase(16) << setiosflags(ios::uppercase) << setfill('0');//循环处理输入的每一行字符串for (string Line; getline(cin, Line) && Line[0] != '#'; cout << endl) {word nGen = 34943, nLen = Line.length(), *pBit = (word*)Bits;//将字符串转存到静态数组中。x86的CPU字节序为little-endian,reverse_copy(Line.begin(), Line.end(), Bits); //反转为正字节序*(word*)(&Bits[nLen]) = 0; //将结尾后面的一些位都清零nLen = nLen / 2 + (nLen % 2 != 0); //计算作为int数组的长度long nRem = 0;//nRem表示余数//循环除所有的位,累加进位的余数,生成CRC码for (int i = nLen - 1; i >= 0; --i) {nRem = ((nRem << 16) + pBit[i]) % nGen;}if (nRem != 0) { //如果余数不为0,则需构造CRC码,算法见文档nRem = nGen - (nRem << 16) % nGen;} //下面按要求的格式输出CRC码unsigned char* pByte = (unsigned char*)&nRem;cout << setw(2) << (int)pByte[1] << ' ' << setw(2) << (int)pByte[0];}return 0;
}

转载于:https://www.cnblogs.com/devymex/archive/2010/08/28/1810480.html

UVa OJ 128 - Software CRC (软件CRC)相关推荐

  1. Windows更新导致AMD Radeon Software等软件无法正常启动

    问题: 解决方法: 方法一: 方法二: 方法三 : 方法四: 这里说一下,作者使用第二种方式成功确保win10不会自动更新驱动,但是有些弊端,电脑监测不到AMD驱动了,所以又找了后面两种方法.使用后面 ...

  2. 2021-06-29 Views and Quality Objectives of Software Construction 软件构造的多维度视图和质量目标

    第一章:Views and Quality Objectives of Software Construction 软件构造的多维度视图和质量目标 目标 1.从三个维度看软件系统的构成 2.用什么样的 ...

  3. [电脑游戏]模拟经营游戏软件公司Software Inc软件公司汉化破解版下载

    电脑游戏:模拟经营游戏软件公司Software Inc软件公司汉化破解版下载 (Software Inc.)软件公司游戏特点: 雇佣员工组成一个团队,让他们从事设计,发展,支持,研发和市场等不同方面的 ...

  4. Linux 大文件crc计算,CRC计算方法与C实现

    CRC的全称为Cyclic Redundancy Check,中文名称为循环冗余校验.它是一类重要的线性分组码,编码和解码方法简单,检错和纠错能力强,在通信领域广泛地用于实现差错控制.实际上,除数据通 ...

  5. 11 Laws of The System Thinking in Software Develo(软件开发中的11个系统思维定律)

    The System Thinking Laws from Peter Senge's book "The Fifth Discipline" applied to Softwar ...

  6. 什么是CRC和CRC检验?

    名词解释: CRC(循环冗余检查)--一种数据传输检错功能,对数据进行多项式计算,并将得到的和数附在帧的后面.接收设备也执行类似的算法. 什么是CRC校验? CRC校验采用多项式编码方法. 被处理的数 ...

  7. ubuntu,debian,software install,软件安装,新手指南,manual

    来之网络,方便学习,摘抄在此,如有侵权,还望海涵. 适用于采用i386安装光盘安装的系统,部分内容适合AMD64和PPC安装. 原文地址:http://wiki.ubuntu.org.cn/%E5%B ...

  8. CRC Calculator(CRC计算器)

    CRC就是循环冗余校验码,这款CRC Calculator就是CRC计算器,也就是循环冗余校验码的计算工具,支持21种计算方法,一键出结果.实现CRC4.CRC5.CRC6.CRC7.CRC8.CRC ...

  9. libmodbus(2)——ERROR CRC received CRC calculated

    一.错误情况 [55][06][00][80][00][00][85][F6] Waiting for a confirmation... <55><02><00> ...

最新文章

  1. 腾讯UED 漂亮的提示信息
  2. 服务器被修改,我的世界
  3. 最新Linux教程发布下载【最后更新4月12日
  4. 自定义控件复选框和单选框的实现
  5. Qt学习笔记-Qt Graphic View Framework
  6. Springpath专注于思科OEM开发工作
  7. c语言先调用load函数,透过源码全流程分析+load函数初始化
  8. 关于.NET异常处理的思考
  9. 已知尺寸计算像素公式
  10. 关于Facebook的28件小事
  11. MySQL year函数
  12. 《一代大商孟洛川》经典台词
  13. jeesite 框架搭建与配置(笔记)
  14. 在eclipse启动tomcat运行一个web程序,报java.lang.OutOfMemoryError: PermGen space
  15. 学习笔记-基于全局和局部对比自监督学习的高分辨率遥感图像语义分割-day1
  16. AOP-纵向横向的区分和基本概念
  17. SPSS 自动线性建模 模型导出方法
  18. ESP8266进阶篇
  19. Transformer解读之:Transformer 中的 Attention 机制
  20. [分享]程序员技术练级攻略

热门文章

  1. 常见的C语言字符串操作
  2. Python3解题:二叉树路径总和问题
  3. linux命令创建硬链接,Linux终端命令接口(十一)硬链接与软连接
  4. redis种类型对应java类型_Redis的五种基本数据类型介绍
  5. mysql权限层级体系_MySQL权限体系介绍
  6. python界面长什么样图片_python界面是什么样的
  7. linux 运行java jar_linux运行jar包、查看jar包进程、停止jar包
  8. 光纤收发器和协议转换器之间有哪些区别?
  9. 飞畅科技 光纤收发器的网管型和非网管型介绍
  10. 工业交换机性能中的“自适应”该如何理解?