2013年9月7日 星期六

UVa 10551 - Basic Remains

Problem link

Keyword: bignum, modulo, base coversion

Algorithm:

First, use strtol() to get the number to perform on modulo operation.
Next, use a for loop to find the remainder after operation.
Last, transform the answer to the specified base.

Code:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>

int main()
{
    int base;
    char n[ 1001 ], m[ 10 ];

    while( scanf( "%d", &base ) && base )
    {
        scanf( "%s %s", n, m );
        int mod = strtol( m, NULL, base ), len = strlen( n ), ans = 0, i = 0;
        char print[ 1000 ] = { 0 };
        for( int i = 0; n[ i ]; ++i )
            n[ i ] -= '0';
        for( int i = 0; i < len; ++i )
        {
            ans *= base;
            ans += n[ i ];
            ans %= mod;
        }
        if( ans == 0 )
        {
            puts( "0" );
            continue;
        }
        while( ans )
        {
            print[ i++ ] = ans % base + '0';
            ans /= base;
        }
        std::reverse( print, print+i );
        puts( print );
    }

    return 0;
}

沒有留言:

張貼留言