Exponentiation
Time Limit: 500MS Memory Limit: 10000K
Total Submissions: 186075 Accepted: 44750
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
using namespace std;
int a[200];
int b[200];
int point,loc,val;
int trans(char s[]) { //把字符串存储到数组a中
memset(a,0,sizeof(a));
int len=strlen(s);
int head=0;
while(s[head]=='0'&&head<len) head++;//标记首0个数
int tail=len-1;
while(s[tail]=='0'&&tail>=0) tail--;
tail=len-1-tail; //标记尾0个数
loc=0;//数组a开始位置
for(int i=head;i<len-tail;i++){
if(s[i]=='.') {
point=len-i-1-tail;//小数点后几位非0
//printf("%d %d %d\n",len,i,tail);
}
else a[loc++]=s[i]-'0';
}
if(loc==0) return -1;
reverse(a,a+loc);
val=0;
for(int i=0;i<loc;i++){
val+=a[i]*pow(10,i);
}
// printf("%d\n",val);
// printf("%d\n",loc);
//printf("%d %d\n",head,tail);
}
void work(int n) {
int j,value;
while(n--){
memset(b,0,sizeof(b));
for(int i=0;i<loc;i++){
value=a[i]*val;
j=i;
while(value){
b[j++]+=value%10;
if(b[j-1]>9){
b[j-1]%=10;
b[j]++;
}
value/=10;
}
}
for(int i=0;i<j;i++){
a[i]=b[i];
}
loc=j;
while(b[loc]!=0){
a[j]=b[loc];
loc++;
}
//for(int i=loc-1;i>=0;i--){
// printf("%d ",a[i]);
//}
//printf("\n");
}
}
int main() {
char R[500];
int n;
while (cin>>R>>n) {
if(trans(R)==-1) printf("0\n");
else{
work(n-1);
if(n*point>=loc){//前面补0
printf(".");
int m=n*point-loc;
while(m--) printf("0");
}
for(int i=loc-1;i>=0;i--){
printf("%d",a[i]);
if(i==n*point&&i!=0) printf(".");
}
printf("\n");
}
}
return 0;
}