• 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 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

    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;
    }
  • 相关阅读:
    memcached学习笔记——存储命令源码分析上篇
    个人理解正则表达式——懒惰匹配
    Symfony2中的设计模式——装饰者模式
    Symfony2源码分析——启动过程2
    Symfony2 HttpKernel事件驱动
    memcached学习笔记——连接模型
    阅读优秀代码是提高开发人员修为的一种捷径
    Linux下部署Symfony2对app/cache和app/logs目录的权限设置
    Symfony2源码分析——启动过程1
    Symfony2 EventDispatcher组件
  • 原文地址:https://www.cnblogs.com/jackge/p/2832365.html
Copyright © 2020-2023  润新知