UVa 128 Software CRC
题意:
给一个字符串,每个字符占一个字节(即256进制数),在后面加两个字节,使得其模34943余数为0
思路:
#include <cstdio>
#include <cstring>
#include <cstring>
const int MAXN = 1200;
int g = 34943;
char b[MAXN];
char s[32] = "0123456789ABCDEF";
int main()
{
while (gets(b))
{
if (b[0] == '#')
break;
long long int k = 0, ans;
for (int i = 0; b[i]; ++i)
k = (k * 256 + b[i]) % g;
ans = (g - (k<<16)%g) % g;
int a[5];
for (int i = 0; i < 4; ++i)
a[i] = ans % 16, ans >>= 4;
printf("%c%c %c%c\n", s[a[3]], s[a[2]], s[a[1]], s[a[0]]);
}
return 0;
}
-------------------------------------------------------
kedebug
Department of Computer Science and Engineering,
Shanghai Jiao Tong University
E-mail: kedebug0@gmail.com
GitHub: http://github.com/kedebug
-------------------------------------------------------