poj1001 Exponentiation

来源:互联网 发布:win10桌面激活windows 编辑:程序博客网 时间:2024/05/20 12:50
Exponentiation
Time Limit: 500MS Memory Limit: 10000KTotal Submissions: 173727 Accepted: 41977

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
思路:先把小数转化成整数,然后在进行高精度计算,最后输出在判断是否要补0,或者删除后缀0,确定小数点的位置,前面Wa的
几发,其中一个数组没有初始化。
代码:
#include <iostream>#include <algorithm>#include <stdio.h>#include <string.h>using namespace std;char c[1000];void poww(char *m,char *n){    int ans[1005];    memset(ans,0,sizeof(ans));    int i,j,k;    int s=strlen(m);    int ss=strlen(n);    for(i=s-1;i>=0;i--)    {        for(j=ss-1,k=s-i-1;j>=0;j--,k++)        {            int sum=(m[i]-'0')*(n[j]-'0')+ans[k];            ans[k]=sum%10;            ans[k+1]+=sum/10;        }    }    for(i=999;i>=0;i--)        if(ans[i]!=0)break;    for(j=0;i>=0;i--)        c[j++]=ans[i]+'0';}int main(){    char a[7];    int n;    char b[1000];    while(~scanf("%s",a))    {        cin>>n;        int i;        int k=0;        int z;        memset(c,0,sizeof(c));        memset(b,0,sizeof(b));        for(i=0;i<6;i++)        {            if(a[i]!='.')            {                b[k++]=a[i];            }            else            {                z=i;            }        }        z=5-z;        z=z*n;        int e=0;        for(i=5;i>=0;i--)        {            if(a[i]!='0')            {                break;            }            else if(a[i]=='0')e++;        }        e=e*n;        n--;        strcpy(c,b);        while(n--)            poww(c,b);        int sum=strlen(c);        if(sum==z)        {            cout<<'.';            for(i=0;i<sum-e;i++)            {                cout<<c[i];            }        }        else if(sum>z)        {            for(i=0;i<sum-e;i++)            {                if(i==sum-z)                {                    cout<<'.'<<c[i];                }                else cout<<c[i];            }        }        else        {            cout<<'.';            for(i=0;i<z-sum;i++)            cout<<0;            for(i=0;i<sum-e;i++)            {                cout<<c[i];            }        }cout<<endl;    }    return 0;}


原创粉丝点击