zoukankan      html  css  js  c++  java
  • uva748

    uva748 - Exponentiation


      Exponentiation 

    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 le 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 Rn. Leading zeros and insignificant trailing zeros should be suppressed in the output.

    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

    套用模版,注意小数的位数不足时要补0,后续0要清掉(其实对底数进行后续0清除就好了,因为出现后续0的唯一可能就是底数有后续0)

    /*
    高精度的幂。幂为低精度。 
    */
    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <climits>
    using namespace std;
    
    #define maxn 30000
    
    struct bign
    {
        int len, s[maxn];
    
        bign()
        {
            memset(s, 0, sizeof(s));
            len = 1;
        }
        
        bign(int num)
        {
            *this = num;
        }
        
        bign(const char* num) 
        {
            *this = num;
        }
        
        bign operator = (int num) 
        {
            char s[maxn];
            sprintf(s, "%d", num);
            *this = s;
            return *this;
        }
        
        bign operator = (const char* num) 
        {
            len = strlen(num);
            for (int i = 0; i < len; i++) s[i] = num[len-i-1] - '0';
            return *this;
        }
        
        string str() const 
        {
            string res = "";
            for (int i = 0; i < len; i++) res = (char)(s[i] + '0') + res;
            if (res == "") res = "0";
            return res;
        }
        
        bign operator + (const bign& b) const
        {
            bign c;
            c.len = 0;
            for (int i = 0, g = 0; g || i < max(len, b.len); i++) 
            {
                int x = g;
                if (i < len) x += s[i];
                if (i < b.len) x += b.s[i];
                c.s[c.len++] = x % 10;
                g = x / 10;
            }
            return c;
        }
        
        void clean() 
        {
            while(len > 1 && !s[len-1]) len--;
        }
        
        bign operator * (const bign& b) 
        {
            bign c; c.len = len + b.len;
            for (int i = 0; i < len; i++)
            for (int j = 0; j < b.len; j++)
            c.s[i+j] += s[i] * b.s[j];
            for (int i = 0; i < c.len-1; i++)
            {
                c.s[i+1] += c.s[i] / 10;
                c.s[i] %= 10;
            }
            c.clean();
            return c;
        }
        
        bign operator - (const bign& b) 
        {
            bign c; c.len = 0;
            for (int i = 0, g = 0; i < len; i++) 
            {
                int x = s[i] - g;
                if (i < b.len) x -= b.s[i];
                if (x >= 0) 
                    g = 0;
                else 
                {
                    g = 1;
                    x += 10;
                }
                c.s[c.len++] = x;
            }
            c.clean();
            return c;
        }
        
        bool operator < (const bign& b) const
        {
            if (len != b.len) return len < b.len;
            for (int i = len-1; i >= 0; i--)
            if (s[i] != b.s[i]) return s[i] < b.s[i];
            return false;
        }
        
        bool operator > (const bign& b) const
        {
            return b < *this;
        }
        
        bool operator <= (const bign& b) 
        {
            return !(b > *this);
        }
        
        bool operator == (const bign& b) 
        {
            return !(b < *this) && !(*this < b);
        }
        
        bool operator != (const bign& b) 
        {
            return (b < *this) || (*this < b);
        }
        
        bign operator += (const bign& b) 
        {
            *this = *this + b;
            return *this;
        }
    };
    
    istream& operator >> (istream &in, bign& x) 
    {
        string s;
        in >> s;
        x = s.c_str();
        return in;
    }
    
    ostream& operator << (ostream &out, const bign& x) 
    {
        out << x.str();
        return out;
    }
    
    int main()
    {
        bign a,ans;
        int b;
        string c;
        
        while (cin >> c >> b)
        { 
            ans = 1;
            
            int index = c.find('.');
            
            if (index != -1)
            {
                //后续0
                for (int i = c.size()-1; i > index; --i)
                {
                    if (c[i] == '0')
                        c.erase(i, 1);
                    else
                        break;
                }
            
                c.erase(index, 1);
            }
            
            index = c.size()-index;
            
            index *= b;
                
            a = c.c_str();
            
            a.clean();
            
            for (int i = 0; i < b; ++i)
            {
                ans = ans*a;
            }
            
            string s = ans.str();
            
            //补0 
            int dif = s.size()-index;
            if (dif >= 0)
            {
                s.insert(s.end()-index, '.');
            }
            else
            {
                for (int i = 0; i < -dif; ++i)
                {
                    s = '0' + s;
                }
                s = '.' + s;
            }
            
            cout << s << endl;
        }
    }
    View Code
  • 相关阅读:
    最终项目 XMessenger Servers
    Linux下patch的制作和应用
    谈移动互联网入口
    绑定服务后台播放音频的简单示例
    MOQL操作数(Operand) (三)
    浅析Hibernate映射(二)——关系映射(3)
    【Extjs优化二】 Form表单提交通用
    Delphi Dll(1)
    用Groovy思考 第三章 Groovy开发环境
    JUnit单元测试入门(三)--JUnit简单实例
  • 原文地址:https://www.cnblogs.com/chenyg32/p/3186343.html
Copyright © 2011-2022 走看看