Exponentiation
Time Limit: 500MS | Memory Limit: 10000K | |
Total Submissions: 95637 | Accepted: 22877 |
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
Hint
If you don't know how to determine wheather encounted the end of input:
s is a string and n is an integer
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 */ { ... }
#include<iostream>
#include<string.h>
#define MAX_SIZE 200
#define MAX_BASE_SIZE 7
using namespace std;
class BigInt{
public:
int data[MAX_SIZE];
int length; // avaliable data length
int position; // dot position
public:
BigInt(){
memset(data, 0, sizeof(data));
length = 1;
position = 0;
}
int getLength(){
return length;
}
int getPosition(){
return position;
}
BigInt operator+(BigInt b){
BigInt result;
memset(result.data, 0, sizeof(result.data));
int resultLength = length > b.length ? length : b.length;
int temp;
int carry;
for(int i = 0; i < resultLength; i++){
temp = data[i] + b.data[i] + carry;
result.data[i] = temp % 10;
carry = temp / 10;
}
if(carry){
result.data[resultLength++] = carry;
}
result.length = resultLength;
return result;
}
BigInt operator*(BigInt b){
BigInt result;
memset(result.data, 0, sizeof(result.data));
result.length = length + b.length;
int i = 0;
for(i = 0; i < length; i++){
for(int j = 0; j < b.length; j++){
result.data[i+j] += data[i] * b.data[j];
}
}
for(i = 0; i < result.length; i++){
result.data[i+1] += result.data[i] / 10;
result.data[i] = result.data[i] % 10;
}
while(result.data[i]){
result.data[i+1] += result.data[i] / 10;
result.data[i] = result.data[i] % 10;
i++;
}
while(i >= 0 && !result.data[i]){
i--;
}
if(i != -1){
result.length = i + 1;
}else{
result.length = 1;
}
result.position = position + b.position;
return result;
}
void operator=(const BigInt& b){
length = b.length;
for(int i = 0; i < length; i++){
data[i] = b.data[i];
}
position = b.position;
}
void print(){
int j,k;
for(j = length -1; j >= position; j--){
// printf("%d", data[j]);
cout << data[j];
}
if(j == -1){
cout << '\n';
return;
}
cout << ".";
if(j < position){
for(k = position -1; k > j; k--){
cout << "0";
}
}
for(; j >= 0; j--){
cout << data[j];
}
cout << "\n";
}
static BigInt cToBigInt(char c[]){
int clen = (int)strlen(c);
int i = 0;
int j = clen -1;
int k;
BigInt result;
memset(result.data, 0, sizeof(result.data));
while(c[i] == '0' && i < clen - 1){
i++;
}
while(c[j] == '0' && j >= 0){
j--;
}
bool isPoint = false;
for(int m = 0; m < clen; m++){
if(c[m] == '.'){
isPoint = true;
}
}
if(j > i){
if(isPoint){
result.length = j - i;
}else{
result.length = j - i + 1;
}
for(j = result.length - 1; c[i] != '.' && j>= 0; j--, i++){
result.data[j] = c[i] -'0';
}
result.position = j + 1;
for(i++; j >= 0; j--, i++){
result.data[j] = c[i] - '0';
}
}else if(j < i){
result.length = 1;
result.position = 0;
}else if(i == j && c[i] != '.'){
for(j = clen -1, k = 0; j >= i; j--, k++){
result.data[k] = c[j] - '0';
}
result.length = k;
result.position = 0;
}
return result;
}
};
int main(){
BigInt R;
BigInt result;
char c[MAX_BASE_SIZE];
int n;
while(cin >> c >> n){
memset(result.data, 0, sizeof(result.data));
result.position = 0;
result.length = 1;
R = BigInt::cToBigInt(c);
result.data[0] = 1;
for(int i = 1; i <= n; i++){
result = result * R;
}
result.print();
}
return 0;
}