zoukankan      html  css  js  c++  java
  • POJ 1001 Exponentiation

    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


    这题是入门题,浮点数相乘,怎么得到其准确值。
    由于题目已经限定了R和n的范围,那么我们就可以在规定的内存范围内申请动态内存。
    解题的主要思路就是乘法的分配率
    如R = 2.3,n = 2,
    则2.3 * 2.3
    =2.3 * (2 + 0.3)
    =2.3 * 2 + 2.3*0.3
    =(2 + 0.3)*2 + (2 + 0.3)*0.3
    =2*2 + 0.3*2 + 2*0.3 + 0.3*0.3
    =4*10^1 + 6*10^(-1) + 6*10^(-1) + 9*10^(-2)  //得到每位的数值
    =4*10^1 + (6 + 6)*10^(-1) + 9*10^(-2)             //相同位的数值相加
    =4*10^1 + 12*10^(-1) + 9*10^(-2)
    =4*10^1 + (10 + 2)*10^(-1) + 9*10^(-2)           //每位的值如果大于等于10,则进行进位
    =(4 + 1)*10^1 + 2*10^(-1) + 9*10^(-2)            //之所以在这里写4+1,因为1是10^(-1)进位所得
    依照上面的规律,我们就可以得到每位(万位、千位、百位、十分位等等)上的数值,然后将每位上的数相加并且进位,依此循环下去。
    再就是在输出时,应该注意输出形式,考虑到各种情况即可。

    #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;
    }
  • 相关阅读:
    Neutron LBaaS Service(2)—— Neutron Services Insertion Model
    Gevent工作原理(转)
    异步IO/协程/数据库/队列/缓存(转)
    IO多路复用(转)
    pytz库时区的坑(转)
    Python3.0的新特性(原创)
    Dockerfile 中的 CMD 与 ENTRYPOINT(转)
    RESTful及API设计(原创)
    RESTful服务最佳实践(转)
    Flask restful源码分析(原创)
  • 原文地址:https://www.cnblogs.com/null00/p/2446005.html
Copyright © 2011-2022 走看看