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

来源:互联网 发布:oracle数据库实训心得 编辑:程序博客网 时间:2024/06/07 18:24

题目是在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 120.4321 205.1234 156.7592  998.999 101.0100 12

Sample Output

548815620517731830194541.899025343415715973535967221869852721.0000000514855464107695612199451127676715483848176020072635120383542976301346240143992025569.92857370126648804114665499331870370751166629547672049395302429448126.76412102161816443020690903717327667290429072743629540498.1075960194566517745610440100011.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';elsesmallNum = 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;}


0 0