【poj1001】 Exponentiation

http://poj.org/problem?id=1001 (题目链接)

题意

  求实数R的n次方,要求高精度。

Solution 

  SB题Wa了一下午,直接蒯题解。 

  高精度,小数点以及去前导后导零很麻烦,而且题目数据很刁钻。 

  注意几个数据: 
  00.000 20 
  0

  000.10 20 
  .00000000000000000001

  .10000 25 
  .0000000000000000000000001

  1 0 
  1

  如果还要数据大话,大牛博客上有。

代码

#include<iostream>
using namespace std;
char str[10];
int n, dot;
int res[999999], a[999999], b[999999];
int len, lena, lenb;

void mul()
{
    int i, j;
    memset(res, 0, sizeof(res));
    for (i=1; i<=lena; i++)
    {
        for (j=1; j<=lenb; j++)
        {
            res[i+j-1] += a[i] * b[j];
            if (res[i+j-1]>9) 
            {
                res[i+j] += res[i+j-1] / 10;
                res[i+j-1] %= 10;
            }
        }
    }
    if (res[lena+lenb-1]>9) 
    {
        res[lena+lenb] += res[lena+lenb-1] / 10;
        res[lena+lenb-1] %= 10;
    }
    lena = lena + lenb;
    for (i=1; i<=lena; i++) a[i] = res[i];
}

int main()
{
    int i, j, up, down;
    while (scanf("%s %d", str, &n)!=EOF)
    {
        dot = -1;
        for (i=5, j=1; i>=0; i--)
        {
            if (str[i]!='.') a[j] = b[j++] = str[i] - '0';
            else dot = i;
        }
        if (dot==-1) lena = lenb = 6;
        else lena = lenb = 5;
        for (i=1; i<n; i++) mul();
        if (dot==-1) 
        {
            for (i=lena; i>=1; i--) printf("%d", a[i]);
            printf("
");
        }
        else
        {
            dot = 5 - dot;
            dot *= n;
            for (i=1; i<=lena; i++)
            {
                if (a[i]!=0)
                {
                    down = i;
                    break;
                }
            }
            for (j=lena; j>=1; j--)
            {
                if (a[j]!=0)
                {
                    up = j;
                    break;
                }
            }
            i = up;
            if (up<dot) i = dot;
            j = down;
            if (j>dot) j = dot + 1;
            for (; i>=j; i--)
            {
                if (i==dot) printf(".");
                printf("%d", a[i]);
            }
            printf("
");
        }
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/MashiroSky/p/5914568.html