HDU 1063 Exponentiation(大数乘法)

Exponentiation

Time Limit: 2000/500 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)


Problem 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
 
/************************************************************************/

题意:给你R和n,求

解题思路:其实这是一个模拟大数乘法的过程,可以转化成n个R相乘,而实数R可以转化成整数p,等计算出最后结果之后再添小数点

小数点后的位数计算方法:假设一开始R小数点后的位数为k位,那么计算结果小数点后应有k*n位,暂时记为s位,那么①如果的位数<s的话,我们就需要在前添小数点再补0,如样例2;如果添了小数点后,小数点之后的位数末尾有0,则需要去掉末尾的0,如样例6

放上一些样例以供参考:

Input
95.123 12
Output
548815620517731830194541.899025343415715973535967221869852721

Input
0.4321 20
Output
.00000005148554641076956121994511276767154838481760200726351203835429763013462401

Input
5.1234 15
Output
43992025569.928573701266488041146654993318703707511666295476720493953024

Input
6.7592 9
Output
29448126.764121021618164430206909037173276672

Input
98.999 10
Output
90429072743629540498.107596019456651774561044010001

Input
1.0100 12
Output
1.126825030131969720661201

Input
.00001 1
Output
.00001

Input
.12345 1
Output
.12345

Input
0001.1 1
Output
1.1

Input
1.1000 1
Output
1.1

Input
10.000 1
Output
10

Input
000.10 1
Output
.1

Input
000000 1
Output
0

Input
000.00 1
Output
0

Input
.00000 0
Output
0

Input
000010 1
Output
10

Input
000.10 1
Output

Input
0000.1 1
Output
.1

Input
00.111 1
Output
.111

Input
0.0001 1
Output
.0001

Input
0.0001 3
Output
.000000000001

Input
0.0010 1
Output
.001

Input
0.0010 3
Output
.000000001

Input
0.0100 1
Output
.01

Input
0.0100 3
Output
.000001

Input
0.1000 1
Output
.1

Input
0.1000 3
Output
.001

Input
1.0000 1
Output
1

Input
1.0000 3
Output
1

Input
1.0001 1
Output
1.0001

Input
1.0001 3
Output
1.003003001

Input
1.0010 1
Output
1.001

Input
1.0010 3
Output
1.003003001

Input
1.0100 1
Output
1.01

Input
1.0100 3
Output
1.030301

Input
1.1000 1
Output
1.1

Input
1.1000 3
Output
1.331

Input
10.000 1
Output
10

Input
10.000 3
Output
1000

Input
10.001 1
Output
10.001

Input
10.001 3
Output
1000.300030001

Input
10.010 1
Output
10.01

Input
10.010 3
Output
1003.003001

Input
10.100 1
Output
10.1

Input
10.100 3
Output
1030.301

Input
99.000 1
Output
99

Input
99.000 3
Output
970299

Input
99.001 1
Output
99.001

Input
99.001 3
Output
970328.403297001

Input
99.010 1
Output
99.01

Input
99.010 3
Output
970593.059701

Input
99.100 1
Output
99.1

Input
99.100 3
Output
973242.271

Input
99.998 1
Output
99.998

Input
99.998 3
Output
999940.001199992


#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<stdlib.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
using namespace std;
const int N = 200;
const int inf = 2000000001;
const int mod = 2009;
char s[11];
int a[N];
int main()
{
    int n,i,j,k,p,l;
    while(~scanf("%s%d",s,&n))
    {
        k=p=0;
        memset(a,0,sizeof(a));
        for(i=j=0;s[i]!='\0';i++)
            if(s[i]!='.')
                p=p*10+s[i]-'0';
            else
                k=strlen(s)-i-1;
        k*=n;
        if(p==0)
        {
            puts("0");
            continue;
        }
        a[j++]=1;
        for(i=0;i<n;i++)
        {
            for(l=0;l<j;l++)
                a[l]=a[l]*p;
            l=0;
            while(a[l]>0||l<j)
            {
                a[l+1]+=a[l]/10;
                a[l]%=10;
                l++;
            }
            j=l;
        }
        if(k>j)
        {
            printf(".");
            for(i=0;i<k-j;i++)
                printf("0");
        }
        for(l=0;l<k&&!a[l];l++);
        for(i=j-1;i>=l;i--)
        {
            if(i+1==k)
                printf(".");
            printf("%d",a[i]);
        }
        puts("");
    }
    return 0;
}
菜鸟成长记


你可能感兴趣的:(ACM,大数)