您的位置:首页 > 其它

UVa 128 - Software CRC

2013-01-28 08:33 471 查看
题意比较难懂,简单说来就是:给你一个字符串,每个字符占一个字节(256进制),后面再加俩个字节,使得字符串的总值可以被34943整除,最后输出这俩个字符的16进制值。

代码如下:

#include <cstdio>
#include <cmath>
#include <cstring>
char str[1030];
const int MAX = 34943;
int main()
{
#ifdef test
freopen("sample.txt", "r", stdin);
#endif
int len;
char s[5];
while(gets(str))
{
if(str[0] == '#')
break;
len = strlen(str);
if(!len)
{
printf("00 00\n");
continue;
}
long long ans = 0;
for(int i=0; i<len; i++)
ans = ((ans << 8) + str[i]) % MAX;
ans = (ans << 16) % MAX;
//需要加的数为34943 - ans,继而再转为16进制;
ans = MAX - ans;
for(int i=3; i>=0; i--)
{
int res = ans % 16;
if(res < 10)
s[i] = res + '0';
else
s[i] = res - 10 + 'A';
ans /= 16;
}
printf("%c%c %c%c\n", s[0], s[1], s[2], s[3]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: