POJ_1001 Exponentiation 题解

一、题目描述

Exponentiation
Time Limit: 500MS Memory Limit: 10000K
Total Submissions: 98225 Accepted: 23570

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

链接:http://poj.org/problem?id=1001

二、解题思路
高精度加法,高精度乘法,高精度阶乘,计算直接按照笔算方法进行计算即可。
三、源代码
  1 #include <iostream>
2 #include <string>
3 #include <cmath>
4
5 using namespace std;
6
7 void swap_string(string &s)
8 {
9 // 将result_tmp反转
10 string result = "";
11
12 for (int i = s.length()-1; i>=0; i--)
13 {
14 result.push_back(s[i]);
15 }
16
17 s = "";
18 s = result;
19 }
20
21 // 高精度整数加法
22 string add(string num1, string num2)
23 {
24 string result = "";
25 int n1 = num1.length();
26 int n2 = num2.length();
27 if (n1 > n2)
28 {
29 string tmp = num1;
30 num1 = num2;
31 num2 = tmp;
32 int itmp = n1;
33 n1 = n2;
34 n2 = itmp;
35 }
36
37 int carry = 0;
38
39 int i,j;
40
41 for (i = n1-1,j = n2-1; i>=0; i--,j--)
42 {
43 int n = num1[i]-48+num2[j]-48+carry;
44 if (n >= 10)
45 {
46 n -= 10;
47 carry = 1;
48 }
49 else
50 {
51 carry = 0;
52 }
53
54 result.push_back(n+48);
55 }
56 if (j+1>=0)
57 {
58 for (;j>=0;j--)
59 {
60 int n = num2[j]-48+carry;
61 if (n >= 10)
62 {
63 n -= 10;
64 carry = 1;
65 }
66 else
67 {
68 carry = 0;
69 }
70 result.push_back(n+48);
71 }
72 if (carry == 1)
73 {
74 result.push_back(49);
75 }
76 }
77 // 将result_tmp反转
78 swap_string(result);
79 return result;
80 }
81
82 // 高精度整数乘法
83 string multiply(string num1, string num2)
84 {
85 int n1 = num1.length();
86 int n2 = num2.length();
87 if (n1 > n2)
88 {
89 string tmp = num1;
90 num1 = num2;
91 num2 = tmp;
92 int itmp = n1;
93 n1 = n2;
94 n2 = itmp;
95 }
96
97 // 始终是的n1为长度较小的那个数
98
99 string total = "0";
100
101 for (int j = n2-1; j>=0; j--)
102 {
103 int carry = 0;
104 string stmp = "";
105 for (int k = 0; k < n2-1-j; k++)
106 {
107 stmp.push_back(48);
108 }
109 for (int i = n1-1; i>=0; i--)
110 {
111 int n = (num1[i]-48)*(num2[j]-48)+carry;
112 carry = n/10;
113 stmp.push_back(n%10+48);
114 }
115 if (carry != 0)
116 {
117 stmp.push_back(carry+48);
118 }
119 else
120 {
121 carry = 0;
122 }
123 swap_string(stmp);
124 string t = add(total,stmp);
125 total = "";
126 total = t;
127 }
128 return total;
129 }
130
131 // 高精度乘方
132 string power(string s, int n)
133 {
134 if (n == 0)
135 {
136 return "1";
137 }
138 else if (n == 1)
139 {
140 return s;
141 }
142 else
143 {
144 string result = "1";
145 for (int i = 0; i < n; i++)
146 {
147 string t = multiply(result,s);
148 result = "";
149 result = t;
150 }
151 return result;
152 }
153 }
154
155
156 int main()
157 {
158 string s = "";
159 int n;
160 while (cin >> s >> n)
161 {
162 int nn = s.length();
163 int index = s.find('.',0);
164 int bitnum = (nn-index-1)*n;
165 s.erase(index,1);
166 string t = power(s,n);
167 t.insert(t.length()-bitnum,".");
168
169 // 对字符串结果进行处理
170 // 前置处理
171 for (int i = 0; t[0]!='.'; i++)
172 {
173 if (t[i] == '0')
174 t.erase(0,1);
175 else
176 break;
177 }
178 // 后置处理
179 for (int i = t.length()-1; t[i]!='.'; i--)
180 {
181 if (t[i] == '0')
182 t.erase(t.length()-1,1);
183 else
184 break;
185 }
186
187 if (t[t.length()-1] == '.')
188 t.erase(t.length()-1,1);
189 cout << t << endl;
190 }
191 return 0;
192 }


转载于:https://www.cnblogs.com/caoxiao/archive/2012/02/28/2371809.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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. 输入说明 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. 输入样例 95.123 12 0.4321 20 5.1234 15 6.7592 9 98.999 10 1.0100 12 输出样例 548815620517731830194541.899025343415715973535967221869852721 .00000005148554641076956121994511276767154838481760200726351203835429763013462401 43992025569.928573701266488041146654993318703707511666295476720493953024 29448126.764121021618164430206909037173276672 90429072743629540498.107596019456651774561044010001 1.126825030131969720661201 小提示 If you don't know how to determine wheather encounted the end of input: 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 */ { ... } 来源 East Central North America 1988 北大OJ平台(代理
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值