BigNums——HDUOJ 1063

来源:互联网 发布:淘宝智能版怎么装修 编辑:程序博客网 时间:2024/06/06 19:00

原题

  • Problem Description

    Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems.
    This problem requires that you write a program to compute the exact value of Rn where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.

  • Input

    The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9.

  • Output

    The output will consist of one line for each line of input giving the exact value of R^n. Leading zeros should be suppressed in the output. Insignificant trailing zeros must not be printed. Don’t print the decimal point if the result is an integer.

  • Sample Input

    95.123 12
    0.4321 20
    5.1234 15
    6.7592 9
    98.999 10
    1.0100 12

  • Sample Output

    548815620517731830194541.899025343415715973535967221869852721
    .00000005148554641076956121994511276767154838481760200726351203835429763013462401
    43992025569.928573701266488041146654993318703707511666295476720493953024
    29448126.764121021618164430206909037173276672
    90429072743629540498.107596019456651774561044010001
    1.126825030131969720661201

这道题很苦逼,不熟练的我,整整写了两天。。。。。。
算法完全靠自己推
解题思路:
1. 记录每次幂循环,小数点数的累计
2. 除去小数点,将数字看作一个整数,进行循环加法进位替代乘法运算
3. 最后就是格式控制好输出。
给几个靠谱的数据试试

数字 幂 结果
001.10 2 1.21
000.01 0 1
011.01 3 1334.633301
000.00 2 0
000000 2 0
00000. 2 0
0.0100 2 .0001
99.999 0 1
1100 2 1210000
.0001 1 .0001
123 2 15129
001.001 2 1.002001
59.000 2 3481

代码:

#include <stdio.h>#include <string.h>#include <string>#include <iostream>using namespace  std;int sum[150];int PowerFunction(string input, int n){    int InputLength = input.length();//数目总长    int DecimalPointLength;          //小数点的位数    int AllDecimalPointLength;       //总的小数点的位数    int i;    int inputFind;    while (input.find("0") == 0)////首先清除整数前导0    {        inputFind = input.find("0");        input.erase(inputFind, 1);        InputLength = input.length();    }    if (InputLength == 0)//全是0    {        AllDecimalPointLength = -1;        return AllDecimalPointLength;    }    if(input.find(".") != -1)//前提得有小数点    {        while (input.find_last_of("0") == InputLength - 1)////首先清除小数末尾0        {            inputFind = input.find_last_of("0");            input.erase(inputFind, 1);            InputLength = input.length();        }        for (i = InputLength - 1; i >= 0; i--)//获取小数点有几位        {            if (input[i] == '.')            {                DecimalPointLength = InputLength - 1 - i;                AllDecimalPointLength = DecimalPointLength;                break;            }        }        /*for (i = DecimalPoint; i <= InputLength - 2; i++)//清除小数点“.”//数据往前挪        {        input[i] = input[i + 1];        }        input[i] = '\0';*///末尾赋值'\0',长度不变【放弃】        input.erase(input.find("."), 1);//string内置函数erase()删除特定字符)                                        //find()找到首次出现的位置    }else//没有小数点    {        AllDecimalPointLength = DecimalPointLength = 0;    }    InputLength = input.length();//更新数目总长度    int tem_sum[150] = {0};    int j, k;    for (i = 0 , j = InputLength - 1; i < InputLength; i++,j--)    {        tem_sum[j] = input[i] - '0';    }    int sum_multi_index;    int sum_add_index;    int tem_sum_length = InputLength;    int TempNum = 0;    if( n == 1)//1次幂函数    {        memcpy(sum, tem_sum, sizeof(tem_sum));        return AllDecimalPointLength;    }    else{        for (i = 2; i <= n; i++)//开始幂运算(注意i=2) + 对小数点位置进行更新        {            memset(sum, 0, sizeof(sum));            sum_add_index = 0;            for (j = InputLength - 1; j >= 0; j--)//input            {                sum_multi_index = sum_add_index;                for (k = 0; k <= tem_sum_length - 1; k++)//tem_sum                {                    TempNum = (sum[sum_multi_index]) + ((input[j] - '0') * tem_sum[k]);                    if (TempNum >= 10)                    {                        sum[sum_multi_index + 1] += TempNum / 10;//取整进位                        sum[sum_multi_index] = TempNum % 10;//取余留下                    }                    else                    {                        sum[sum_multi_index] = TempNum;                    }                    sum_multi_index++;                }                sum_add_index++;            }            for(int jj = sizeof(sum) / sizeof(sum[0]) - 1; jj >= 0; jj--)            {                if(sum[jj] != 0)                {                    tem_sum_length = jj + 1;                    break;                }            }                 for (int kk = sizeof(sum) / sizeof(sum[0]) - 1; kk >= 0; kk--)            {                tem_sum[kk] = sum[kk];            }            AllDecimalPointLength += DecimalPointLength;        }    }    if (InputLength == 0)//除了.全是0    {        AllDecimalPointLength = -1;    }    return AllDecimalPointLength;}int main(){    string input;    int n;    int DecimalPointLength;    while (cin>>input)    {        scanf("%d", &n);        if (n == 0)//0次幂函数        {            printf("1\n");            continue;        }        DecimalPointLength = PowerFunction(input, n);        int kk;        for (kk = sizeof(sum) / sizeof(sum[0]) - 1; kk >= 0; kk--)        {            if(sum[kk]!=0)            {                break;            }        }        if (kk == -1)//所有数都是0        {            kk = 0;        }        int IsZero = false;//判断整数部分是否为0        if (kk < DecimalPointLength)//小数向前进位,缺0补0,说明整数部分为0        {            kk = DecimalPointLength;            IsZero = true;        }        for (; kk >= 0; kk--)        {            if (DecimalPointLength == kk && kk != 0)//需要判断没有小数点,kk不是最后一个            {                if (IsZero == true)                {                    printf(".");                }                else                {                    printf("%d", sum[kk]);                    printf(".");                }            }else            {                printf("%d", sum[kk]);            }        }        printf("\n");    }}