每日一題2014/7/30

Exponentiation
Time Limit: 500MS   Memory Limit: 10000K
Total Submissions: 134035   Accepted: 32776

Descriptionios

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 R n where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.

Inputui

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.

Outputspa

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 Inputcode

95.123 12
0.4321 20
5.1234 15
6.7592  9
98.999 10

1.0100 12

Sample Outputblog

548815620517731830194541.899025343415715973535967221869852721
.00000005148554641076956121994511276767154838481760200726351203835429763013462401
43992025569.928573701266488041146654993318703707511666295476720493953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201

沒作出來,抄的別人的代碼,一開始思路差很少,可是我代碼水平太差了

#include<iostream>
using namespace std;

#define LENGTH 200
class BigNumber 
{
private:
    int* value; 
    int decimal_bits;
    int exp;

    int previousBit(int bit) {
        if (bit < 10)
            return 0;
        else
            return bit / 10;
    }
public:
    BigNumber(char* str, int e) {
        value = new int[LENGTH];
        //decimal = new int[LENGTH];
        for(int i = 0; i < LENGTH; i++)
        {
            value[i] = 0;
            //decimal[i] = 0;
        }
        int index = -1;
        int str_len = strlen(str);
        for(int i = 0; i < str_len; i++)
            if(str[i] == '.')
                index = i;
        decimal_bits = (index == -1 ? 0 : str_len - index - 1);
        //將輸入的字符串轉化爲相應的數值
        if (index == -1)
        {
            for (int i = 0; i < str_len; i++)
                value[LENGTH - str_len + i] = str[i] - '0';
        }
        else
        {
            for (int i = 0; i < index; i++)
                value[LENGTH - str_len + i + 1] = str[i] - '0';
            for (int i = index + 1; i < str_len; i++)
                value[LENGTH - str_len + i] = str[i] - '0';
        }
        exp = e;
    }

    ~BigNumber()
    {
        delete[] value;
    }
    void pow() {
        if (exp == 0) {
            for (int i = 0; i < LENGTH - 1; i++)
                value[i] = 0;
            value[LENGTH - 1] = 1;
            return;
        }

        int* tmp_value = new int[LENGTH];
        //int* tempDecimal = new int[LENGTH];
        for(int i = 0; i < LENGTH; i++)
            tmp_value[i] = value[i];

        for (int i = 0; i < exp - 1; i++) 
        {
            //乘一次保存一次值
            int* result_value = new int[LENGTH];

            for(int j = 0; j < LENGTH; j++)
                result_value[j] = 0;
            
            for (int j = 0; j < LENGTH; j++)
            {
                if (tmp_value[j] != 0)
                {
                    for (int k = 0; k < LENGTH; k++)
                    {
                        if (value[k] != 0)
                            result_value[j + k - LENGTH + 1] += tmp_value[j]*value[k];
                    }
                }
            }
            for (int j = 0; j < LENGTH; j++)
                value[j] = result_value[j];

            //進行進位,以防在上述相乘的過程當中溢出
            for (int j = LENGTH - 1; j >=0; j--)
            {
                value[j - 1] += previousBit(value[j]);
                value[j] %=10;
            }

            delete result_value;
        }

        delete[] tmp_value;

    }

    void print()
    {
        if (exp == 0)
            printf("%d\n", 1);
        else
        {
            if (decimal_bits == 0)
            {
                bool flag = false;
                for (int i = 0; i < LENGTH; i++)
                {
                    if (value[i] == 0 && !flag)
                        continue;
                    else
                    {
                        flag = true;
                        printf("%d", value[i]);
                    }
                }
                printf("\n");
            }
            else
            {
                int bits = decimal_bits*exp;
                bool is_zero = true;
                for (int i = 0; i < LENGTH - bits; i++)
                {
                    if (value[i] == 0 && is_zero)
                        continue;
                    is_zero = false;
                    printf("%d", value[i]);
                }
                bool output_dot = false;
                int output_pos = -1;
                for (int i = LENGTH - 1; i >= LENGTH - bits; i--)
                {
                    if (value[i] == 0 && !output_dot)
                        continue;
                    output_dot = true;
                    output_pos = i;
                    break;
                }
                if (output_dot)
                {
                    printf(".");
                    for (int i = LENGTH - bits; i <=output_pos; i++)
                        printf("%d", value[i]);
                }
                printf("\n");
            }
        }
    }
};

int main() {
    char* str=new char[6];
    int n;

    while(cin>>str>>n)
    {
        BigNumber* bn = new BigNumber(str, n);
        bn->pow();
        bn->print();
        delete bn;
    }
    return 0;
}
相關文章
相關標籤/搜索