128 - Software CRC
Time limit: 3.000 seconds
128 - Software CRC
Time limit: 3.000 secondsYou 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.
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 GenerationThe 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.
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.
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).
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).
Sample Input
this is a test A #
Sample Output
77 FD 00 00 0C 86
题目大意:
给你一个字符串(可能有空格,最多1024字符),每个字符代表一个数字,因为最多有256个字符,所以这个字符串其实是表示一个256进制的数字。但这还没完,在这个256进制数字要再加两位,组成一个新的256进制数字,这个数字要能被g=34943(这是个十进制数字)整除,要你输出新添加的那两个数字是什么,这两个数字要以16进制的形式输出,用空格隔开
解题方法:
把原来的256进制数字先转为十进制数x,那么再添加两位后,其实原本的数字翻了2倍,即翻了 *256*256=65536,所以x=x*65536。把新添加的两位数转化为十进制y。那么新的数字其实是x+y,我们的目的就是(x+y)%g=0; 根据模运算的性质可转化为 (x+y)%g=(x%g+y%g)%g=0; 我们要知道的是y,y=(g-x%g)%g,再把y转为16进制即可
源代码:
#include <cstdio>
#include <cstring>
#define N 1030
#define g 34943
char num[N], c[17] = "0123456789ABCDEF";
int main() {
while (gets(num) && num[0] != '#') {
long long t = 0, ans = 0;
for (int i = 0; num[i]; i ++) {
t = (t * 256 + num[i]) % g;
}
t = (t * 65536) % g;
ans = (g - t) % g;
int a[5];
for (int i = 0; i < 4; i ++) {
a[i] = ans % 16;
ans /= 16;
}
printf("%c%c %c%c\n", c[a[3]], c[a[2]], c[a[1]], c[a[0]]);
}
return 0;
}

本文介绍了一种基于CRC(循环冗余校验)的错误检测方法,并提供了一个具体的实现案例。该方法通过附加校验值确保数据传输过程中的准确性。

1120

被折叠的 条评论
为什么被折叠?



