uva128 - 软件CRC

不算的对原串进行取余,,

做后结合余数得出CRC码

r是最后的余数,m是34943,

因为(r*256*256+c)%m==0

所以最小的c是使得(r*256*256+c)==m的值

所以c = m-(r*256*256)%m

代码如下:

#include <cstdio>
#include <cstring>
#define M 1100
char s[M];
const int m = 34943;
int solve(int len)
{
    long long ans = 0;
    for(int i = 0; i < len; i++)
        ans = (ans*256+s[i])%m;
    return ans%m;
}
int main ()
{
    while(gets(s))
    {
        int len = strlen(s);
        if(len==1&&s[0]=='#') break;
        if(len==0) {printf("00 00\n"); continue;}
        long long ans = solve(len);
        ans = (m - (256*256*ans%m))%m;
        int aa[10] = {0,0,0,0};
        for(int i = 0; ans; i++)
        {
            int t;
            aa[i] = ans%16;
            ans /= 16;
        }
        printf("%X%X %X%X\n",aa[3],aa[2],aa[1],aa[0]);
    }
    return 0;
}


你可能感兴趣的:(uva128 - 软件CRC)