首页 > 代码库 > poj1001(高精度乘法)

poj1001(高精度乘法)

1.题目表述

                                    Exponentiation
Time Limit: 500MS Memory Limit: 10000K
Total Submissions: 135893 Accepted: 33256

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

Hint

If you don‘t know how to determine wheather encounted the end of input: s is a string and n is an integer
C++ while(cin>>s>>n) { ... } c while(scanf("%s%d",s,&n)==2) //to  see if the scanf read in as many items as you want /*while(scanf(%s%d",s,&n)!=EOF) //this also work    */ { ... }

 2.题目大意

  给两个数n,R,求n^R.

 3.解题方法

  高精度乘法或者java的大整数类

 4.代码:

  1 #include <iostream>  2 #include <cmath>  3 #include <algorithm>  4 #include <cstring>  5 #include <cstdio>  6 #define debug 0  7 using namespace std;  8 void chen(char a[],char b[])  9 { 10     int i,j,k,l,sum,c[410]={0}; 11     l=strlen(a)+strlen(b); 12     for(i=strlen(b)-1;i>=0;i--) 13     for(j=strlen(a)-1,k=i+j+1;j>=0;j--,k--){ 14         sum=(b[i]-0)*(a[j]-0)+c[k]; 15         c[k]=sum%10;c[k-1]+=sum/10; 16     } 17     for(i=c[0]?0:1,j=0;i<l;i++) 18     a[j++]=(c[i]+0); 19     a[j]=0; 20 } 21 int main() 22 { 23     int r,n,i,j,point,len,d; 24     char s[100],s1[100],c[1000000]; 25     while (scanf("%s%d",&s,&n)!=EOF){ 26         point=0; 27         len=strlen(s); 28         memset(s1,0,sizeof(s1)); 29         i=0; 30         while (s[i]==0&&i<len){//开始没有预处理这个,遇到0000 1 挂了 31             i++; 32         } 33         d=0; 34         while (i<=len){ 35             s[d]=s[i]; 36             i++; 37             d++; 38         } 39         len=strlen(s); 40         while (s[point]!=.&&point<len){ 41             point++; 42         } 43         i=point; 44         while (i<len){ 45             s[i]=s[i+1]; 46             i++; 47         } 48         point=len-point-1; 49         point=point*n; 50         if (n!=0){ 51             strcpy(c,s); 52             for (i=2;i<=n;i++){ 53                 chen(c,s); 54             } 55             i=point; 56             len=strlen(c); 57             d=0; 58             while (s[d]==0&&d<len){ 59                 d++; 60             } 61             if (d>=len){printf("0");}else 62             if (i>len-d){ 63                 printf("."); 64                 for (j=1;j<=i-(len-d);j++){ 65                     printf("0"); 66                 } 67                 len=len-1; 68                 while (c[len]==0){ 69                     len--; 70                 } 71                 for (i=d;i<=len;i++){ 72                     printf("%c",c[i]); 73                 } 74             }else if (i==len-d){ 75                 printf("."); 76                 len=len-1; 77                 while (c[len]==0){ 78                     len--; 79                 } 80                 for (i=d;i<=len;i++){ 81                     printf("%c",c[i]); 82                 } 83             }else { 84                 i=len-i; 85                 len=len-1; 86                 while (c[len]==0){ 87                     len--; 88                 } 89                 for (j=d;j<i+d;j++){ 90                     printf("%c",c[j]); 91                 } 92  93                 if (len>=j){//这里考虑错了0010.00 1 竟然输出“.”,后来改过后就好了. 94                     printf("."); 95                     for (j=i+d;j<=len;j++){ 96                     printf("%c",c[j]); 97                     } 98                 } 99             }100             printf("\n");101         }else {102             printf("1\n");103         }104     }105     return 0;106 }

 

poj1001(高精度乘法)