北京大学1001ACM——高精度类型题总结

题目描述:

Exponentiation
Time Limit: 500MS   Memory Limit: 10000K
Total Submissions: 80517   Accepted: 19098
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:数据的接收和存取:

运算因子超出了整型、实型能表示的范围,肯定不能直接用一个数的形式来表示。可选方案:数组和字符串。

1,推荐用字符串接收数据,然后把接收的字符转化为整形赋值到整型数组中。,

2,相乘时,做好数据的逆序处理,因为按照我们算的步骤,数据应该在数组中是逆序存储。

基本问题2:计算结果位数的确定

乘积的位数最大为两个因子的位数之和

 乘法代码:(没看太懂)

#include <iostream>
using namespace std;
int main()
{
    char r[6]={'0'};//底数 
    int n=1;//指数 
    while(cin>>r>>n)
    {
        int m[250]={0};
        int b[6]={0};
        int i,j,m_i;
        int m_number=0,m_data=0;//小数点个数,每次相乘的数据 
        for(i=0;i<6;i++)//找到小数点的位置 
        {
            if(r[i]=='.')
            {
                m_i=i;
                break;
            }
        }
        if(i==6)//说明没有小数点
        {
            j=0;
            while(r[j]=='0')
            {
                r[j]='a';
                j++;
            }
            m_number=0;
        }
        else//说明有小数点 
        {
            i=0;
            while(i<m_i&&r[i]=='0')//小数点之前前导0处理为-1
            {
                r[i]='a';
                i++;
            }
            i=5;
            while(i>m_i&&(!r[i]||r[i]=='0'))//小数点之后的后缀零处理 
            {
                r[i]='a';//赋值为a 
                i--;
            }
            for(i=5;i>=0;--i)
            {
                             if(!r[i]||r[i]=='a')continue;
                             if(r[i]=='.')break;
                             m_number++;//判断有多少小数 
            }
        }
        for(i=0,j=0;i<6;++i)
        {
            if(r[i]=='a')continue;
            if(r[i]=='.')continue;
            if(!r[i])continue;
            b[j]=(int)(r[i]-'0');//转成int的b 
            j++;
        }
        while(j<6)
        {
            b[j]=-1;
            j++;
        }//b可以弄出实数来了
        for(i=0;i<6;++i)
        {
                        if(b[i]==-1)continue;
                        m_data=m_data*10+b[i];//相乘的大数 
        } 
        for(j=5,i=250-1;j>=0&&i>=0;--j)
        {
                         if(b[j]==-1)continue;
                         else
                         {
                             m[i]=b[j];//赋值给m 
                             i--;
                         }
        }
        m_number=m_number*n;
        n--;
        while(n--&&i!=249)//如果出现输入都是0 
        {
                  for(j=i+1;j<250;j++)//有这么多位 
                        {
                                  int tem=m[j]*m_data;//每一位与数相乘 
                                  m[j]=0;
                                  if(tem==0)m[j]=0;
                                  else
                                  {
                                      int t=j;
                                      while(tem/10!=0||tem%10!=0)
                                      {
                                             m[t]=m[t]+tem%10;//将每一位都分离出来存放在不同的单元 
                                             int tt1=t;
                                             while(m[tt1]>9)//对超过9的处理 
                                             {
                                                 int tem2=m[tt1];
                                                 m[tt1]=tem2%10;//得到这位的数据 
                                                 tt1--;
                                                 m[tt1]=m[tt1]+tem2/10;//往前加进的位数 
                                             }
                                             t--; 
                                             tem=tem/10;        
                                      }
                                  }
                        }
                        i=0;
                        while(m[i]==0)i++;//找到m中第一个非零的位置 
                        i--;
              }       
              i=0;
              while(i<250&&m[i]==0)
                i++;
              if(m_number==0)//对没有小数点的处理 
              {
                             if(i==250)cout<<0<<endl;
                             else
                             {
                                 for(;i<250;++i)cout<<m[i];
                                 cout<<endl;
                             }
              }
              else//有小数点的处理 
              {
                  if(m_number>250-i)//250-i表示在i之前有i个零,有250-i个非零 
                  {//小数点超过相乘得到的实数,则补零 
                    cout<<".";
                    while(m_number-->250-i)
                        cout<<0;
                    for(;i<250;i++)
                        cout<<m[i];
                    cout<<endl; 
                  }
                  else 
                  {
                       for(;i<250;++i)
                       {
                                      if(m_number==250-i)cout<<".";//包含处理等于 
                                      cout<<m[i];
                       }
                       cout<<endl;
                  }
              }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/lyxcode/p/11160733.html