高精度浮点数幂次方 POJ 1001 Exponentiation

题目是在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 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


给定一个浮点数,和一个整数,求这个数的n次幂,要求前后不能有0。

乍一看挺简单,这是一道大数的题目,浮点数求幂指数,题目给了输入数据的范围,其实这个范围没有也是可以做的,可能通过这个范围可以更加快捷的计算,暂时没发现,欢迎Programmer们前来指教

如果这道题目用Java进行计算就简单多了,引用包,不多说。

public class poj1001 {      
    public static void main(String[] args) {  
        Scanner sc =new Scanner(System.in);  
        while(sc.hasNext()){  
            BigDecimal a = sc.nextBigDecimal();  
            int b =sc.nextInt();  
            a=a.pow(b);  
            String s=a.stripTrailingZeros().toPlainString();  
            if(s.startsWith("0."))  
                s=s.substring(1);  
            System.out.println(s);            
        }  
    }  
}  
作为Acmer 有必要用c解一下,刚开始想到了整数小数分开进行阶乘,试了一下根本不符合逻辑,果断放弃。后来想到了将小数转换成整数,然后用万进制进行计算最后在讲计算得出的结果转化成小数的形似,前提是需要计算出小数后面的位数。

#include <stdio.h>
#include <string.h>
#include <math.h>
int a,n,smallNum;
long long numList[100000];
int index = 1;
int output();
int input() {
	a = 0;
	char str[10];
	if(~scanf("%s%d",str,&n)) {
		smallNum = 0;
		index = 1;
		for(int i =0 ; i<6; i++)
			if(str[i]!='.')
				a=a*10+str[i]-'0';
			else
				smallNum = 5-i;
		return 1;
	}
	return 0;
}

int solve() {
	memset(numList,0,sizeof(numList));
	numList[0] = a;
	for(int i =1 ; i<n; i++){
		for(int j = 0; j<index; j++)
			numList[j]*=a;
		for(int j = 0; j<index; j++)
			if(numList[j]>=1000000) {
				numList[j+1]+=numList[j]/1000000;
				numList[j]%= 1000000;
			}
		if(numList[index])
			index++;
	}
	return 0;
}

int output() {
//	for(int i = index-1;i>=0;i--)
//		printf("%d ",numList[i]); 
//	printf("\n");
	char strNum[10000];
	int k=0;
	int end=0;
	for(int i = index-1; i>=0; i--)
	{
		for(int j = 5;j>=0;j--)
		{	
			strNum[k] = (numList[i]/(int)pow(10,j))%10+'0';
			if(strNum[k]!='0')
				end = k;
			k++;
		}
	}
	strNum[k] = '\0';
//	printf("%s\n",strNum); 
	int i = 0;
	int point = strlen(strNum)-smallNum*n;
	while(strNum[i]=='0' && i<point)
		i++;
	if(point<0)
	{
		printf(".");
		while(point<-1)
		{
			printf("0");
			point++;
		}
		printf("0");
	}
	while(i<=end)
	{
//		printf(">>>>%d %d\n",i,point); 
		if(i == point)
			printf(".");
		printf("%c",strNum[i]);
		i++;
	}
	while(i<point)
	{
		printf("0");
		i++;
	}
	printf("\n");
	return 0;
}

int main() {
	while(input()) {
		solve();
		output();
	}
	return 0;
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值