POJ1001

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 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(0.0 < R < 99.999),要求写出程序精确计算R的n次方(Rn),其中n是整数并且0 < n <= 25。

【输入格式】

输入包括多组R和n。R的值占第1到第6列,n的值占第8和第9列。

【输出格式】

对于每组输入,要求输出一行,该行包含精确的R的n次方。输出需要去掉前导的0和后面不要的0。如果输出是整数,不要输出小数点。

分析:
这道题就是一个高精度的低精度幂的问题。
我直接设置一个累乘器,累乘求的幂(高精度的乘法就按照模板用的结构体完成)
然后就是确立小数点的位置
由于数组是倒序输出(c.lend-1代表最高位),因此要去掉c.d[0]开始往后且小数点位置往前所有的零
然后如果是0.xxxx输出应该是.xxxx所以需要判断最高位也就是(c.d[lend-1]是不是0)
最后注意如果是10.0000因输出10

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
struct Rn {
char R[5500];
int n;
};
struct sd {
int lend,d[5500];
    sd(){memset(d,0,sizeof(d));}
    sd operator * (const sd & x)const {
        sd c;
        for(int i=0;i<lend;i++) {
            for(int j=0;j<x.lend;j++) {
             c.d[i+j]+=d[i]*x.d[j];
             c.d[i+j+1]+=c.d[i+j]/10;
             c.d[i+j]%=10;
            }
        }
    int t=lend+x.lend-1;
    while(c.d[t]!=0) t++;
    c.lend=t;
    return c;
    }
};
int main(void) {
Rn X;
while(scanf("%s%d",X.R,&X.n)!=EOF) {
int len=strlen(X.R);
sd a;int p=0;
int dot;
    for(int i=0;i<len;i++)
        if(X.R[i]=='.')
            dot=i;
    int dotx=len-dot-1;
    int dotm=dotx*X.n;
    for(int i=len-1;i>=0;i--)
        if(X.R[i]!='.')
        a.d[p++]=X.R[i]-48;
    a.lend=p;
sd c;
c.d[0]=1; c.lend=1;
    for(int i=0;i<X.n;i++)
       c=c*a;
    int zero=0,j=0;
    int dotmp=dotm;
    while(j<dotm&&c.d[j]==0) {zero++; j++;}
    dotm-=zero;
    if(c.d[c.lend-1]!=0) {
        for(int i=c.lend-1;i>=dotmp;i--)
            printf("%d",c.d[i]);
    }
    if(dotm!=0)
        printf(".");
    for(int j=dotmp-1;j>=zero;j--)
        printf("%d",c.d[j]);
    printf("\n");
}
return 0;
}


//注:POJ不接受#include<bits/stdc++.h>这个万能头文件
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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. 输入说明 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 小提示 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 */ { ... } 来源 East Central North America 1988 北大OJ平台(代理

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值