Exponentiation
Time Limit: 500MS | Memory Limit: 10000K | |
Total Submissions: 134035 | Accepted: 32776 |
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> using namespace std; #define LENGTH 200 class BigNumber { private: int* value; int decimal_bits; int exp; int previousBit(int bit) { if (bit < 10) return 0; else return bit / 10; } public: BigNumber(char* str, int e) { value = new int[LENGTH]; //decimal = new int[LENGTH]; for(int i = 0; i < LENGTH; i++) { value[i] = 0; //decimal[i] = 0; } int index = -1; int str_len = strlen(str); for(int i = 0; i < str_len; i++) if(str[i] == ‘.‘) index = i; decimal_bits = (index == -1 ? 0 : str_len - index - 1); //将输入的字符串转化为相应的数值 if (index == -1) { for (int i = 0; i < str_len; i++) value[LENGTH - str_len + i] = str[i] - ‘0‘; } else { for (int i = 0; i < index; i++) value[LENGTH - str_len + i + 1] = str[i] - ‘0‘; for (int i = index + 1; i < str_len; i++) value[LENGTH - str_len + i] = str[i] - ‘0‘; } exp = e; } ~BigNumber() { delete[] value; } void pow() { if (exp == 0) { for (int i = 0; i < LENGTH - 1; i++) value[i] = 0; value[LENGTH - 1] = 1; return; } int* tmp_value = new int[LENGTH]; //int* tempDecimal = new int[LENGTH]; for(int i = 0; i < LENGTH; i++) tmp_value[i] = value[i]; for (int i = 0; i < exp - 1; i++) { //乘一次保存一次值 int* result_value = new int[LENGTH]; for(int j = 0; j < LENGTH; j++) result_value[j] = 0; for (int j = 0; j < LENGTH; j++) { if (tmp_value[j] != 0) { for (int k = 0; k < LENGTH; k++) { if (value[k] != 0) result_value[j + k - LENGTH + 1] += tmp_value[j]*value[k]; } } } for (int j = 0; j < LENGTH; j++) value[j] = result_value[j]; //进行进位,以防在上述相乘的过程中溢出 for (int j = LENGTH - 1; j >=0; j--) { value[j - 1] += previousBit(value[j]); value[j] %=10; } delete result_value; } delete[] tmp_value; } void print() { if (exp == 0) printf("%d\n", 1); else { if (decimal_bits == 0) { bool flag = false; for (int i = 0; i < LENGTH; i++) { if (value[i] == 0 && !flag) continue; else { flag = true; printf("%d", value[i]); } } printf("\n"); } else { int bits = decimal_bits*exp; bool is_zero = true; for (int i = 0; i < LENGTH - bits; i++) { if (value[i] == 0 && is_zero) continue; is_zero = false; printf("%d", value[i]); } bool output_dot = false; int output_pos = -1; for (int i = LENGTH - 1; i >= LENGTH - bits; i--) { if (value[i] == 0 && !output_dot) continue; output_dot = true; output_pos = i; break; } if (output_dot) { printf("."); for (int i = LENGTH - bits; i <=output_pos; i++) printf("%d", value[i]); } printf("\n"); } } } }; int main() { char* str=new char[6]; int n; while(cin>>str>>n) { BigNumber* bn = new BigNumber(str, n); bn->pow(); bn->print(); delete bn; } return 0; }
每日一题2014/7/30