题目链接 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.
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
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
*************************************/