Poj Solution 1715

http://poj.org/problem?id=1715

#include <stdio.h>
#include <algorithm>
using namespace std;


char *digit = "FEDCBA9876543210"; //0123456789ABCDEF";
int p[8][8];
bool sign[16];

int main()
{
    int i,j,n,k;
    char ans[9];


    for( k=0; k<8; k++ )
    {
        p[k][7-k] = 1;
        for( i=6-k; i>=0; i-- )
            p[k][i] = p[k][i+1]*(15-i);
    }
    

    for( i=0; i<16; i++ )
        sign[i] = 0;

    scanf( "%d", &n );

    n -- ;
    for( k=0; n >= p[k][0]*15; k++ )
        n -= p[k][0]*15;

    int h;
    for( i=0; i<8-k; i++ )
    {
        h = n / p[k][i];
        for( j=0; j<16; j++ )
        if( !sign[j] )
        {
            if( !h )break;
            h--;
        }

        ans[i] = digit[j];
        sign[j] = 1;
        n %= p[k][i];
    }

    ans[i] = '';

    printf( "%sn", ans );

    return 0;
}
											
This entry was posted in poj. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *