【百炼oj】1001:Exponentiation

描述

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 Rnwhere R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.

输入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.输出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.样例输入
95.123 12
0.4321 20
5.1234 15
6.7592  9
98.999 10
1.0100 12
样例输出
548815620517731830194541.899025343415715973535967221869852721
.00000005148554641076956121994511276767154838481760200726351203835429763013462401
43992025569.928573701266488041146654993318703707511666295476720493953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201


#include<stdio.h>
#include<string.h>
#include<math.h>
#define p 7
#define len 120
int main(){
    int mult(int*a,int*b);
    int n,i,j,k,location,flag;
    int an[len],bn[len];
    char sz[p];
    while(scanf("%s %d",sz,&n)==2){
            memset(an,0,sizeof(an));
            memset(bn,0,sizeof(bn));
            an[0]=1;
            location=-1;
        for(i=0;i<p-1;i++){
            if(sz[i]=='.'){
                location=i;//memory the location of the dot
                location=n*(p-2-i);//get the new location
                for(j=i;j<p-1;j++)
                    sz[j]=sz[j+1];//remove the dot
                    sz[p-1]='\0';
            }
        }
        for(i=0;i<strlen(sz);i++)
            bn[i]=sz[strlen(sz)-1-i]-'0';
             for(i=0;i<n;i++)
            mult(an,bn);
        if(location==-1){
        flag=0;
        for(i=len-1;i>=0;i--){
            if(flag)printf("%d",an[i]);
        else if(an[i]){
            printf("%d",an[i]);
            flag=1;
            }
        }
         if(!flag)printf("0");
         printf("\n");
        }//整数情况
        else//小数
        {
            for(i=0;;i++){
                 k=i;
                if(an[i]!=0)break;
            }
           for(i=len-1;i>=k;i--){
               if(an[i])break;
           }
           if(i<location){
            printf(".");
            for(i=location-1;i>=k;i--)printf("%d",an[i]);
            printf("\n");
           }
           else{
                if(k<location){
            flag=0;
            for(i=len-1;i>=k;i--){
            if(flag){
                    printf("%d",an[i]);
                    if(i==location&&i!=k)printf(".");
            }
            else if(an[i]){
            printf("%d",an[i]);
            if(i==location&&i!=k)printf(".");
            flag=1;
            }
        }
                    printf("\n");
           }
           else{
            flag=0;
            for(i=len-1;i>=location;i--){
            if(flag){
                    printf("%d",an[i]);
            }
            else if(an[i]){
            printf("%d",an[i]);
            flag=1;
            }
        }
          if(!flag)printf("0");
          printf("\n");
           }
           }
        }
    }
}
int mult(int*a,int*b){
    int i,j,k,l;
    int c[len];
    memset(c,0,sizeof(c));
    for(i=0;i<len;i++){
        k=0;
        for(j=0;j<len-i;j++){
            l=c[i+j]+a[j]*b[i]+k;
            c[i+j]=l%10;
            k=l/10;
        }
    }
    for(i=0;i<len;i++)a[i]=c[i];
}

PKU OJ的一道题目--Exponentiation(ID:1001)????????

04-12

哪位做出来啦??最后的数据不知道怎么处理,而且我做出来的结果根本没那么大的精度。rnExponentiationrnTime Limit: 500MS Memory Limit: 10000K rnTotal Submissions: 47367 Accepted: 10904 rnrnDescriptionrnrnProblems involving the computation of exact values of very large magnitude(['mæɡnitju:d],大小, 积, 量, 长(度)) and precision([pri'siӡən],精确(性); 精密(度)) are common. For example, the computation of the national debt is a taxing experience for many computer systems. rnrnThis 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. rnrnInputrnrnThe input will consist of a set of pairs of values for R and n. The R value will occupy columns(['kɔləm]圆柱, 柱壮物,专栏) 1 through 6, and the n value will be in columns 8 and 9.rnrnOutputrnrnThe output will consist of one line for each line of input giving the exact value of R^n. Leading zeros should be suppressed([sə'pres],止住,不让发表 )in the output. Insignificant([͵insiɡ'nifikənt]无意义的; 无关重要的; 无用的) trailing([treil]痕迹, 足迹,一串, 一系列) zeros must not be printed. Don't print the decimal(['desiməl]以十作基础的, 十进位的, 小数的) point if the result is an integer.rnSample Inputrnrn95.123 12rn0.4321 20rn5.1234 15rn6.7592 9rn98.999 10rn1.0100 12rnrnSample Outputrnrn548815620517731830194541.899025343415715973535967221869852721rn.00000005148554641076956121994511276767154838481760200726351203835429763013462401rn43992025569.928573701266488041146654993318703707511666295476720493953024rn29448126.764121021618164430206909037173276672rn90429072743629540498.107596019456651774561044010001rn1.126825030131969720661201rn

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭