高精度浮点数计算 poj 1001 Exponentiation

poj 1001 Exponentiation

Time LimitMemory LimitTotal SubmissionsAccepted
500ms10000k18516444572

Descrimination
  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.

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

题目大意
  涉及精度非常高的的计算问题是很常见的。像是国家债务的计算对许多计算机系统来说就是一种费力的体验。
  这个问题要求你写一个计算一个精度很高的数 Rn 的程序。其中,R 是一个实数(0.0 < R < 99.999),而 n 是一个整数(0 < n <= 25)。

输入
  输入包括几对 R 和 n 的值,R占输入的1到6列,而 n 则占8到9列。

输出
  输出将由每行输入的一行构成,给出输出中 Rn 的精确值,如果小数点前面只有0,则舍去。同时不能打印不重要的尾随零。如果结果是整数,则不要打印小数点。

分析
  在我们直接对数据进行计算时,我们无法达到题目结果要求的精度。这道题主要是考察我们大数乘法。大数乘法主要有模拟手工计算的普通大数乘法,分治算法和FFT算法。下面采用的是模拟手工计算的大数乘法。
  在我们进行模拟手工乘法的大数运算时,例如样例中的95.123是下图中的状况

     95.123
*     95.123
     285 369

这样我们就可以将小数点去掉,当做整数来进行运算,运算时需要注意的是我们需要注意的是应该先从末位进行计算以方便进位,这时候我们就可以现将数据进行倒置,然后计算。在输出结果是再将其倒置,以得到正确的结果。

//大数乘法
#include<stdio.h>
#include<string.h>

int main()
{
	char R[6];
	int n,pot,rePot;
	short re[200],miRe[200],Rn[5];
	while(scanf("%s %d",R,&n)==2){
		int len=0;
		int i,j,k,m;
		for(i=0;i<200;i++) re[i]=miRe[i]=0;
		for(i=0;i<5;i++) Rn[i]=0;
		for(i=0;i<6;i++){
			if(R[i]=='.') {
				pot=i;
				break;
			}
		}
		rePot=(5-pot)*n;
		for(i=0;i<6;i++){
			if(i<5-pot) Rn[i]=miRe[i]=re[i]=R[5-i]-'0';
			else if(5-pot<=i<5) Rn[i]=re[i]=miRe[i]=R[4-i]-'0';
			if (i>=5) re[i]=miRe[i]=0;
		}
		while(n>=2){
			n--;
			for(m=0;m<200;m++) re[m]=0;
			for(j=0;j<5;j++){  //乘数 
				short c,t;
				for(k=0;k<200;k++){     //被乘数 
					if(Rn[j]==0) break;
					c=miRe[k]*Rn[j];
					re[k+j]+=c;
                    for(t=k+j;re[t]>9;t++)
                    {
                        re[t+1]+=re[t]/10;
                        re[t]=re[t]%10;
                    }
				}
			}
			for(m=0;m<200;m++) miRe[m]=re[m];
		}
		int firstindex=-1;
		for(i=199;i>=rePot;i--)
        {
            if(re[i]>0)
            {
                firstindex=i;
                break;
            }
        }
        int lastindex=-1;
        for(i=0;i<rePot;i++)
        {
			if(re[i]>0){
				lastindex=i;
				break;
			} 
        }
        if(firstindex!=-1){
        	for(i=firstindex;i>=rePot;i--){
        		printf("%d",re[i]);
			}
		}
		if(lastindex!=-1){
			printf(".");
			for(i=rePot-1;i>=lastindex;i--){
				printf("%d",re[i]);
			}
		}
		printf("\n");
	} 
	return 0;
 } 
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值