PKU1001 Exponentiation

Exponentiation

http://poj.org/problem?id=1001

Time Limit: 500MS Memory Limit: 10000K
Total Submissions: 111912 Accepted: 27180

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

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    */
 {
 ...
 }

Source

 
 
解法一:java(没大懂)
 
import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.math.BigDecimal;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
 public class Main {
 
     public static void main(String[] args) {
         try {
             BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
             String data = null;
             while ((data = reader.readLine()) != null) {
                 String[] d = data.split("\\s+");
                 BigDecimal r = new BigDecimal(d[0]);
                 int n = Integer.parseInt(d[1]);
                 BigDecimal result = r.pow(n);
                 String res = result.toPlainString();
                 int i = 0;
                 for (; i < res.length(); i++)
                     if (res.charAt(i) != '0')
                         break;
                 int j=res.length()-1;
                 for(; j>=i; j--)
                     if(res.charAt(j)!='0')
                         break;
                 if(res.charAt(j)=='.')
                     j--;
                 System.out.println(res.substring(i, j+1));
             }
         } catch (IOException ex) {
             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
         }
     }
 }

 

解法二:模拟

 

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
    int count,n,point,i,j,k,mul[6],mul1[200],mul2[200];
    char digit[10],num[10];
    while(scanf("%s%d",digit,&n)!=EOF){
        count=0;memset(mul1,0,sizeof(mul1));
        for(i=5;i>=0;i--)
            if(digit[i]!='0')break;
        for(j=i;j>=0;j--)
            if(digit[j]=='.')point=i-j;//求小数点
            else if(digit[j]>='0'&&digit[j]<='9'){
                mul[count]=digit[j]-'0';
                mul1[count++]=digit[j]-'0';
            }//end of else if 除去小数点
        for(i=1;i<n;i++)
        {
            memset(mul2,0,sizeof(mul2));
            for(j=0;j<200;j++)
                for(k=0;k<count;k++)
                    mul2[j+k]+=mul1[j]*mul[k];
            for(j=0;j<200;j++)
            {
                if(mul2[j]>=10)
                {
                    mul2[j+1]+=mul2[j]/10;
                    mul2[j]%=10;
                }
            }
            for(j=0;j<200;j++)
                mul1[j]=mul2[j];
        }//end of for
        for(j=199;j>=0;j--)
            if(mul1[j]!=0)break;
        if(j==-1)
        {
            puts("0");
            continue;
        }
        if(j<n*point)
        {
            putchar('.');
            for(k=1;k<n*point-j;k++)
                putchar('0');
            for(k=j;k>=0;k--)
                printf("%d",mul1[k]);
        }//end of if
        else 
        {
            for(k=j;k>=0;k--)
            {
                if(k==n*point-1)putchar('.');
                printf("%d",mul1[k]);
            }
        }//end of else 
        putchar('\n');
    }//end of while
    return 0;
}

 

转载于:https://www.cnblogs.com/jackge/archive/2012/12/25/2832365.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值