Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

finnaly,有代码有真相

Posted by dengxianzhi at 2012-10-07 18:01:31 on Problem 1001
弄了一整天终于看到了accept,差点放弃了

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

#define LEN 250
#define COU 6


int getSize(double, int*);
void getResult(int, int, int, int*);
int a2f(char*, int*, int);

int main()
{

    int n, size = 0;
    int result[LEN];
    int tmp = 0;
    char a[COU+1];
    double ret ;
    int exp = 1;
    int i;
    
    while(scanf("%s %d", a, &n) == 2)
    {
        if(n == 0)
        {
            printf("1\n");
            continue;
        }
        
        memset(result, 0, sizeof(int)*LEN);

        tmp = a2f(a, &size, COU);
        //   printf("tmp = %d, size = %d\n", tmp, size);
        if(tmp == 0)
        {
            printf(".\n");
            continue;
        }
        getResult(tmp, n, size, result);

    }
    return 0;
}


int getSize(double r, int* tmp)
{
    int count = 0;

    while((int)r != r)
    {
        count++;

        r *= 10;
    }

    *tmp = (int)r;
    return count;
}

void getResult(int tmp, int p, int size, int* result)
{

    int p_size = p*size;
    int arr_size = 1;
    int i;
    int carry = 0;
    
    result[0] = 1;

    while(p > 0)
    {

        for(i = 0; i < arr_size; i++)
        {
            result[i] = result[i] * tmp;
        }
        for(i = 0; i < arr_size; i++)
        {
            result[i] += carry;
            if(result[i] >= 10)
            {
                carry = result[i] / 10;
                result[i] = result[i]%10;
            }
            else
            {
                carry = 0;
            }
        }
        while(carry >= 10)
        {
            result[arr_size++] = carry%10;
            carry = carry/10;
        }
        if(carry > 0)
        {
            result[arr_size++] = carry;
            carry = 0;
        }
        p--;
            
    }


    if(p_size >= arr_size)
    {
        printf(".");
        while(p_size > 0)
        {
            printf("%d", result[p_size-1]);
            p_size--;
        }
        printf("\n");
    }
    else
    {
        while(arr_size > 0)
        {
            if(arr_size == p_size)
                printf(".");
            printf("%d", result[arr_size-1]);
            arr_size--;
        }
        printf("\n");
    }
    
    
    
}


int a2f(char* a, int* count, int len)
{

    int i = 0, j = 0;
    int _int = 0, _flo = 0;
    int exp = 1;
    int  result = 0;
    

    for(i = 0; i < len; i++)
    {
        if(a[i] == '.')
            break;
    }
    
    *count = len - i -1;

//    if(*count < 0)
//    {
//        *count = len;
//        i = -1;
//    }

    for(j = 0; j < i; j++)
    {
        _int = (a[j] - '0') + _int*10;

    }
    for(j = i+1; j < len; j++)
    {
        _flo = (a[j] - '0') + _flo*10;
        exp *= 10;
    }


    result = _int*exp + _flo;

    if(result == 0)
        return 0;
    while((*count > 0) && (result%10 == 0))
    {
        result = result/10;
        *count = *count-1;
    }
    
    return result;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator