问题描述
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 Rnwhere R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.要求
- 输入
- 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. 输出
- 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.
-
思路
本题为高精度计算问题,需要将输入的字符串转换为数组,按照四则运算的方式进行运算,具体代码如下所示:
1 #include
2 #include 3 #include 4 5 using namespace std; 6 7 struct bign{ 8 int data[1000]; 9 int length; 10 int point; 11 bign(){ 12 memset(data, 0, sizeof(data)); 13 length = 0; 14 point = 0; 15 } 16 }; 17 18 string str; 19 int n; 20 21 bign Change(){ 22 bign a; 23 a.point = str.length() - str.find('.') - 1; 24 int j = 0; 25 for(unsigned int i=0;i =zero;i--){ 82 if(zero==rs.point){ 83 ans.insert(ans.end(), rs.data[i] + '0'); 84 } 85 else{ 86 if(i==rs.point){ 87 ans.insert(ans.end(), rs.data[i] + '0'); 88 ans.insert(ans.end(), '.'); 89 } 90 else{ 91 ans.insert(ans.end(), rs.data[i] + '0'); 92 } 93 } 94 } 95 } 96 else{ 97 ans.insert(ans.end(), '.'); 98 for(int i=rs.point-1;i>=zero;i--){ 99 ans.insert(ans.end(), rs.data[i] + '0');100 }101 }102 cout< < >str>>n){107 bign bg = Change();108 bign rs = power(bg, n);109 show(rs);110 }111 return 0;112 } 在解题过程中,遇到的是尾部零处理和首部零处理问题,要注意的是,除了题中所给的样例,10.00之类的幂次需要考虑,这类测试数据,在尾部零处理中,不应该含小数点。