HDU 1063 Exponentiation&&POJ 1001 Exponentiation

22 篇文章 1 订阅
13 篇文章 0 订阅

题目链接 HDU 1063: http://acm.hdu.edu.cn/showproblem.php?pid=1063

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

Exponentiation

Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7189    Accepted Submission(s): 2049


Problem 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
题目大意:给你一个小数x,和整数n,叫你计算x^n的精确值。高精度乘法,用C++模拟乘法还要处理各种字符串问题,对人的耐心以及编程能力都有很大的考验,想当年第一次码这道题的时候是 2014-06-17 15:04:50,足足码了一个下午,愣是WA了4,5次,最后总算坚持下来仔细查错,然后AC了。其中滋味,只有你码过才知道。如今又用JAVA搞了一番,唉,不想多说了,用JAVA做,这道题就是水题,这可能也是用JAVA搞ACM很少的优势之一吧。

AC的C++代码如下:

<pre name="code" class="cpp">#include<string>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<list>
#include<sstream>
using namespace std;
const int L=100005;
int sum;
string mul(string a,string b)
{
    string s;
    int na[L],nb[L],nc[2*L],len_a=a.size(),len_b=b.size();
    fill(na,na+L,0);fill(nb,nb+L,0);fill(nc,nc+2*L,0);
    for(int i=len_a-1,j=1;i>=0;i--,j++) na[j]=a[i]-'0';
    for(int i=len_b-1,j=1;i>=0;i--,j++) nb[j]=b[i]-'0';
    for(int i=1;i<=len_a;i++)
        for(int j=1;j<=len_b;j++)
            nc[i+j-1]+=na[i]*nb[j];
    for(int i=1;i<=(len_a+len_b);i++)
        nc[i+1]+=nc[i]/10,nc[i]%=10;
    if(nc[len_a+len_b]) s+=nc[len_a+len_b]+'0';
    for(int i=len_a+len_b-1;i>=1;i--) s+=nc[i]+'0';
    return s;
}
string toInter(string s)
{
    for(int i=s.size()-1;s[i]!='.';i--)
        if(s[i]=='0') s.erase(i,1);
        else break;
    for(int i=0;s[i]!='.';)
        if(s[i]=='0') s.erase(i,1);
        else break;
    int pos=s.find('.');
    if(pos!=string::npos)
    {
        sum=s.size()-pos-1;
        s.erase(pos,1);
    }
    return s;
}
void printResults(string s)
{
    if(s.size()<sum)
    {
        cout<<".";
        for(int i=1;i<=sum-s.size();i++) cout<<0;
        cout<<s<<endl;
    }
    else
    {
        if(sum) s.insert(s.size()-sum,".");
        cout<<s<<endl;
    }
}
int main()
{
    string a,b;
    string s,r="1";
    int n;
   /* while(cin>>a>>b)
    {
        a=toInter(a);
        cout<<a<<endl;
        b=toInter(b);
        cout<<b<<endl;

        cout<<mul(a,b)<<endl;

        cout<<"sum = "<<sum<<endl;
    }*/
    while(cin>>s>>n)
    {
        r="1";
       s=toInter(s);
       sum=sum*n;
       for(int i=1;i<=n;i++)
           r=mul(r,s);
        //r=GetResults(r);
       //cout<<r<<endl;
       printResults(r);
       //cout<<sum<<endl;
    }
    return 0;
}
/*********************************************
Source Code

Problem: 1001		User: W5u2Y0ing
Memory: 1796K		Time: 32MS
Language: C++		Result: Accepted
Source Code
*************************************************/

 下面是AC这道题的JAVA代码。 

import java.util.*;
import java.math.*;
import java.io.*;
public class Main {
    public static void main(String args[]){
        Scanner cin=new Scanner(new BufferedInputStream(System.in));
        BigDecimal ans;
        int n;
        while(cin.hasNext()){
            ans=cin.nextBigDecimal();
            n=cin.nextInt();
            String s=ans.pow(n).stripTrailingZeros().toPlainString();
            if(s.charAt(0)=='0') s=s.substring(1);
            System.out.println(s);
        }
    }
}
/*******************************
Problem : 1063 ( Exponentiation )     Judge Status : Accepted
RunId : 12373842    Language : Java    Author : W5u2Y0ing
Code Render Status : Rendered By HDOJ Java Code Render Version 0.01 Beta
*************************************/


 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值