zoukankan      html  css  js  c++  java
  • POJ 1001 Exponentiation

    Exponentiation
    Time Limit: 500MS   Memory Limit: 10000K
    Total Submissions: 171995   Accepted: 41641

    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

    测试数据:试试你能不能过!

    0001.1 1 
    0001.0 1
    0010.0 1
    010.01 1
    010.1 1
    .1 1
    1. 1
    .01 1 
    .10 1
    .010 1
    .0101 1
    1.0 1
    1.02 1
    1.020 1
    01.0 1
    20.010 1

    注意:当n为1的情况,坑爹啊,这个疏忽折腾得很不爽!!!

    源码
    版本一
      1 #include<iostream>
      2 #include<string>
      3 //#include<ctime>
      4 //#include<fstream>
      5 using namespace std;
      6 int main()
      7 {
      8     //clock_t start=clock();
      9     //ifstream in("test.txt");
     10     //ofstream out("output.txt");
     11     string r;
     12     int n;
     13     int *res=new int[151];
     14     int *temp=new int[151];
     15     int *zhi=new int[151];
     16     int i,j,k,l;
     17     int res_len;
     18     while(cin>>r>>n)
     19     {
     20         memset(res,0,sizeof(int)*151);
     21         memset(temp,0,sizeof(int)*151);
     22         memset(zhi,0,sizeof(int)*151);
     23         int loc=0;
     24         j=1;
     25         for(i=r.size()-1;i>=0;i--)
     26             if(r[i]=='.')
     27                 loc=r.size()-(i+1);
     28             else
     29                 zhi[j++]=r[i]-'0';
     30         loc=loc*n;
     31         int zhi_len=j--;
     32         int temp_len=zhi_len;
     33         res_len=0;
     34         memcpy(temp,zhi,sizeof(int)*(zhi_len+1));
     35         n--;
     36         while(n)
     37         {
     38             for(i=1;i<=zhi_len;i++)
     39             {
     40                 int jw=0,ls;
     41                 for(j=1;j<=temp_len;j++)
     42                 {
     43                     ls=zhi[i]*temp[j]+res[j+i-1]+jw;
     44                     jw=ls/10;
     45                     res[j+i-1]=ls%10;
     46                 }
     47                 if(jw)
     48                 {
     49                     res[j+i-1]=jw;
     50                     res_len=j+i;
     51                 }
     52                 else
     53                     res_len=j+i-1;
     54             }
     55             memcpy(temp,res,sizeof(int)*(res_len+1));
     56             temp_len=res_len;
     57             n--;
     58             if(n)
     59             {
     60                 memset(res,0,sizeof(int)*(res_len+1));
     61                 res_len=0;
     62             }
     63         }
     64         if(res_len==0)
     65             memcpy(res,zhi,sizeof(int)*(zhi_len+1));//考虑n为1的情况
     66         for(i=150;i>=1;i--)//从高位开始查找非零数字,去掉前导零
     67             if(res[i]||i==(loc+1))//找到非零数字或者便利到小数点的位置
     68                 break;
     69         int qian=i;//标记找到的高位
     70         for(i=1;i<=150;i++)//从低位开始查找非零数字,去掉尾随零
     71             if(res[i]||i==(loc+1))//找到非零数字或者便利到小数点的位置
     72                 break;
     73         int hou=i;//标记找到的低位
     74         for(i=qian;i>=hou;i--)//从高位开始遍历到低位
     75         {
     76             if(i==(loc+1))//该位为小数点位置
     77             {
     78                 if(i==qian)//小数点位置上的数可能是0可能非0
     79                 {
     80                     if(i!=hou)//小数点后还有非零数字
     81                     {
     82                         if(res[i])//小数点位置上不是0
     83                             cout<<res[i];//输出作为整数部分
     84                         cout<<'.';//输出小数点
     85                     }
     86                     else//小数点位置上的数可能是0可能非0,如果是0不符合输入的要求,一定不是0
     87                         cout<<res[i];//输出小数点位上的数字,最终结果为一位整数
     88                 }
     89                 else//小数点前还有非0数字
     90                 {
     91                     cout<<res[i];//输出小数点上的数字,作为整数部分的个位
     92                     if(i!=hou)//小数点后还有非0数字,有效的小数部分
     93                         cout<<'.';//输出小数点
     94                 }    
     95             }//该位不是小数点的位置,输出该位数字
     96             else
     97                 cout<<res[i];
     98         }
     99         cout<<endl;
    100     }
    101     delete []res;
    102     //clock_t end=clock();
    103     //cout<<"Running time: "<<(double)(end-start)<<endl;
    104     return 0;
    105 }

    版本二

    #include<iostream>
    #include<string>
    #include<ctime>
    #include<fstream>
    using namespace std;
    int main()
    {
        //ifstream in("test.txt");
        //ofstream out("output.txt");
        string r;
        int n;
        int *res=new int[151];
        int *temp=new int[151];
        int *zhi=new int[151];
        int i,j,k,l;
        int res_len;
        while(cin>>r>>n)
        {
            for(i=1;i<=150;i++)
                res[i]=temp[i]=zhi[i]=0;
            //处理输入
            //r="99999";n=25;
            int loc=0;
            j=1;
            for(i=r.size()-1;i>=0;i--)
                if(r[i]=='.')
                    loc=r.size()-(i+1);
                else
                    zhi[j++]=r[i]-'0';
            loc=loc*n;
            //处理结束
            int zhi_len=j--;
            int temp_len=zhi_len;
            res_len=0;    
            for(i=1;i<=temp_len;i++)
                temp[i]=zhi[i];
            n--;
            while(n)//n大于等于2计算,否则直接输出res
            {    
                for(i=1;i<=zhi_len;i++)
                {
                    int jw=0,ls;
                    for(j=1;j<=temp_len;j++)
                    {
                        ls=zhi[i]*temp[j]+res[j+i-1]+jw;
                        jw=ls/10;
                        res[j+i-1]=ls%10;
                    }
                    if(jw)
                    {
                        res[j+i-1]=jw;
                        res_len=j+i;
                    }
                    else
                        res_len=j+i-1;
                }
                for(i=1;i<=res_len;i++)
                    temp[i]=res[i];
                temp_len=res_len;
                n--;
                if(n)    //res清空
                {
                    for(i=1;i<=res_len;i++)
                        res[i]=0;
                    res_len=0;
                }
                
            }
            if(res_len==0)
                for(i=1;i<=zhi_len;i++)
                    res[i]=zhi[i];
            for(i=150;i>=1;i--)
                if(i==(loc+1)||res[i])//是小数点的位置
                    break;
            int qian=i;
            for(i=1;i<=150;i++)
                if(i==(loc+1)||res[i])
                    break;
            int hou=i;
            for(i=qian;i>=hou;i--)
            {
                if(i==(loc+1))
                {
                    if(i==qian)
                    {
                        if(i!=hou)
                        {
                            if(res[i])
                                cout<<res[i];
                            cout<<'.';
                        }
                        else
                            cout<<res[i];
                    }
                    else
                    {
                        cout<<res[i];
                        if(i!=hou)        
                            cout<<'.';
                    }    
                }
                else
                    cout<<res[i];
            }
            cout<<endl;
            
        }
        delete []res;
        return 0;
    }
  • 相关阅读:
    Protocol handler start failedCaused by: java.net.SocketException: Permission denied
    springboot无法获取证书内容
    IDEA中使用git合并分支的过程报错:cant checkout because of unmerged files
    IDEA中使用git报错Permission denied (publickey)
    linux常用的操作命令
    启动Tomcat报错:A child container failed during start
    linux下搭建redis内网端口映射工具-rinetd
    linux(centos)下安装supervisor进程管理工具
    SDBCI-WRCF2020-MI赛题成绩回顾
    Google搜索技巧
  • 原文地址:https://www.cnblogs.com/zhaopeng938/p/7545698.html
Copyright © 2011-2022 走看看