ACM (UVa) : 748


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int convert (char r [])
{
    int dot;
    int length = strlen (r);

    for ( int i = 0; i < length; i++ ) {
        if ( r [i] == '.' )
            dot = i;
    }

    for ( int i = dot; i < length - 1; i++ )
        r [i] = r [i + 1];

    r [length - 1] = 0;

    return atoi (r);
}

int main ()
{
    char input [12];

    while ( gets (input) ) {
        char r [8];
        int n;

        sscanf (input, "%s %i", r, &n);

        int result [100];
        int r_length = strlen (r);
        int index = 0;
        int dot_position;

        while ( r [index] == '0' ) { // leading zero
            for ( int i = index + 1; r [i] != 0; i++ )
                r [i - 1] = r [i];
            r [r_length - index - 1] = 0;
            r_length--;
        }

        index = 0;

        while ( r [r_length - 1] == '0' ) { // trailing zero
            r [r_length - 1] = 0;
            r_length--;
        }

        for ( int i = r_length - 1; i >= 0; i-- ) {
            if ( r [i] == '.' )
                dot_position = r_length - i - 1;
            else
                result [index++] = r [i] - '0';
        }

        int carry;
        int number = convert (r);
        int j;

        for ( int i = 1; i < n; i++ ) {
            carry = 0;
            for ( j = 0; j < index; j++ ) {
                carry = result [j] * number + carry;
                result [j] = carry % 10;
                carry /= 10;
            }

            while ( carry ) {
                result [j++] = carry % 10;
                carry /= 10;
            }

            index = j;
        }

        dot_position *= n;
        dot_position = index - dot_position;

        while ( dot_position < 0 ) {
            result [index++] = 0;
            dot_position++;
        }

        for ( int i = index - 1; i >= 0; i-- ) {
            if ( dot_position-- == 0 )
                printf (".");
            printf ("%d", result [i]);
        }
        printf ("\n");
    }

    return 0;
}

One thought on “ACM (UVa) : 748

  1. Critical Input : 
    0.4321 1
    0.4321 2
    0.4321 3
    0.4321 4
    0.4321 5
    0.0001 3
    000.01 5
    99.999 25
    
    
    Critical Ouput:
    .4321
    .18671041
    .080677568161
    .0348607772023681
    .01506334182914325601
    .000000000001
    .0000000001
    99975002999770012649468717709519310815545705768715.426520024799744573673126042964184298069822900531298735002299997000002499999
    
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s