SPOJ 362 Ignore the Garbage 转7进制+简单大数除法

SPOJ 362 Ignore the Garbage 转7进制+简单大数除法

显然正着倒着看仍然是数字的只有7个数:0,1,2,5,6,8,9

所以就是用这7个数组合成不同的数。

将n转换成7进制,对应位输出即可。

 

#include <cstdio>

#include <cstring>

#include <cstdlib>

#include <algorithm>



using namespace std;



const char num[] = "0125986";

const int MAXN = 1010;

const int BASE = 7;



char str[MAXN];

int a[MAXN];

int b[MAXN];

int ans[MAXN];

int cnt, len;



bool check()

{

    for ( int i = 0; i < len; ++i )

        if ( a[i] ) return true;

    return false;

}



void Mod()

{

    int tmp = 0;

    for ( int i = 0; i < len; ++i )

    {

        tmp = tmp * 10 + a[i];

        b[i] = tmp / BASE;

        tmp %= BASE;

    }

    ans[cnt++] = tmp;



    int i = 0, j;

    while ( i < len && b[i] == 0 ) ++i;

    for ( j = 0; i < len; ++i, ++j )

        a[j] = b[i];

    len = j;



    return;

}



int main()

{

    int T;

    scanf( "%d", &T );

    while ( T-- )

    {

        scanf( "%s", str );

        len = strlen(str);

        cnt = 0;

        for ( int i = 0; i < len; ++i )

            a[i] = str[i] - '0';



        while ( check() ) Mod();



        for ( int i = 0; i < cnt; ++i )

            putchar( num[ ans[i] ] );



        puts("");

    }

    return 0;

}

 

你可能感兴趣的:(poj)