hdu 1063 Exponentiation(大数模版题)
求浮点数的幂(0<mi<=25),整数部分为0时,0不输出;整数部分无前缀0,小数部分无后缀0
特殊的样例:
0 :
00.00 12
.000 12
前缀0:
001.10 12
001 12
后缀0:
0.0100 12
用大数模版。将浮点数转换整数,记录小数点位置,得到运算结果再将大数转换成字符串处理输出。
附大牛的模版
http://blog.csdn.net/hackbuteer1/article/details/6595881
http://blog.csdn.net/Hackbuteer1/article/details/6595901
1 #include<iostream>
2 #include<string>
3 #include <cstring>
4 #include<iomanip>
5 #include<algorithm>
6 using namespace std;
7
8 #define MAXN 9999
9 #define MAXSIZE 10
10 #define DLEN 4
11
12 class BigNum
13 {
14 private:
15 int a[500]; //可以控制大数的位数
16 int len; //大数长度
17 public:
18 BigNum(){ len = 1;memset(a,0,sizeof(a)); } //构造函数
19 BigNum(const int); //将一个int类型的变量转化为大数
20 BigNum(const char*); //将一个字符串类型的变量转化为大数
21 BigNum(const BigNum &); //拷贝构造函数
22 BigNum &operator=(const BigNum &); //重载赋值运算符,大数之间进行赋值运算
23
24 friend istream& operator>>(istream&, BigNum&); //重载输入运算符
25 friend ostream& operator<<(ostream&, BigNum&); //重载输出运算符
26
27 BigNum operator+(const BigNum &) const; //重载加法运算符,两个大数之间的相加运算
28 BigNum operator-(const BigNum &) const; //重载减法运算符,两个大数之间的相减运算
29 BigNum operator*(const BigNum &) const; //重载乘法运算符,两个大数之间的相乘运算
30 BigNum operator/(const int &) const; //重载除法运算符,大数对一个整数进行相除运算
31
32 BigNum operator^(const int &) const; //大数的n次方运算
33 int operator%(const int &) const; //大数对一个int类型的变量进行取模运算
34 bool operator>(const BigNum & T)const; //大数和另一个大数的大小比较
35 bool operator>(const int & t)const; //大数和一个int类型的变量的大小比较
36
37 void print(); //输出大数
38 char* tochar(char*);
39 };
40 BigNum::BigNum(const int b) //将一个int类型的变量转化为大数
41 {
42 int c,d = b;
43 len = 0;
44 memset(a,0,sizeof(a));
45 while(d > MAXN)
46 {
47 c = d - (d / (MAXN + 1)) * (MAXN + 1);
48 d = d / (MAXN + 1);
49 a[len++] = c;
50 }
51 a[len++] = d;
52 }
53 BigNum::BigNum(const char*s) //将一个字符串类型的变量转化为大数
54 {
55 int t,k,index,l,i;
56 memset(a,0,sizeof(a));
57 l=strlen(s);
58 len=l/DLEN;
59 if(l%DLEN)
60 len++;
61 index=0;
62 for(i=l-1;i>=0;i-=DLEN)
63 {
64 t=0;
65 k=i-DLEN+1;
66 if(k<0)
67 k=0;
68 for(int j=k;j<=i;j++)
69 t=t*10+s[j]-'0';
70 a[index++]=t;
71 }
72 }
73 BigNum::BigNum(const BigNum & T) : len(T.len) //拷贝构造函数
74 {
75 int i;
76 memset(a,0,sizeof(a));
77 for(i = 0 ; i < len ; i++)
78 a[i] = T.a[i];
79 }
80 BigNum & BigNum::operator=(const BigNum & n) //重载赋值运算符,大数之间进行赋值运算
81 {
82 int i;
83 len = n.len;
84 memset(a,0,sizeof(a));
85 for(i = 0 ; i < len ; i++)
86 a[i] = n.a[i];
87 return *this;
88 }
89 istream& operator>>(istream & in, BigNum & b) //重载输入运算符
90 {
91 char ch[MAXSIZE*4];
92 int i = -1;
93 in>>ch;
94 int l=strlen(ch);
95 int count=0,sum=0;
96 for(i=l-1;i>=0;)
97 {
98 sum = 0;
99 int t=1;
100 for(int j=0;j<4&&i>=0;j++,i--,t*=10)
101 {
102 sum+=(ch[i]-'0')*t;
103 }
104 b.a[count]=sum;
105 count++;
106 }
107 b.len =count++;
108 return in;
109
110 }
111 ostream& operator<<(ostream& out, BigNum& b) //重载输出运算符
112 {
113 int i;
114 cout << b.a[b.len - 1];
115 for(i = b.len - 2 ; i >= 0 ; i--)
116 {
117 cout.width(DLEN);
118 cout.fill('0');
119 cout << b.a[i];
120 }
121 return out;
122 }
123
124 BigNum BigNum::operator+(const BigNum & T) const //两个大数之间的相加运算
125 {
126 BigNum t(*this);
127 int i,big; //位数
128 big = T.len > len ? T.len : len;
129 for(i = 0 ; i < big ; i++)
130 {
131 t.a[i] +=T.a[i];
132 if(t.a[i] > MAXN)
133 {
134 t.a[i + 1]++;
135 t.a[i] -=MAXN+1;
136 }
137 }
138 if(t.a[big] != 0)
139 t.len = big + 1;
140 else
141 t.len = big;
142 return t;
143 }
144 BigNum BigNum::operator-(const BigNum & T) const //两个大数之间的相减运算
145 {
146 int i,j,big;
147 bool flag;
148 BigNum t1,t2;
149 if(*this>T)
150 {
151 t1=*this;
152 t2=T;
153 flag=0;
154 }
155 else
156 {
157 t1=T;
158 t2=*this;
159 flag=1;
160 }
161 big=t1.len;
162 for(i = 0 ; i < big ; i++)
163 {
164 if(t1.a[i] < t2.a[i])
165 {
166 j = i + 1;
167 while(t1.a[j] == 0)
168 j++;
169 t1.a[j--]--;
170 while(j > i)
171 t1.a[j--] += MAXN;
172 t1.a[i] += MAXN + 1 - t2.a[i];
173 }
174 else
175 t1.a[i] -= t2.a[i];
176 }
177 t1.len = big;
178 while(t1.a[len - 1] == 0 && t1.len > 1)
179 {
180 t1.len--;
181 big--;
182 }
183 if(flag)
184 t1.a[big-1]=0-t1.a[big-1];
185 return t1;
186 }
187
188 BigNum BigNum::operator*(const BigNum & T) const //两个大数之间的相乘运算
189 {
190 BigNum ret;
191 int i,j,up;
192 int temp,temp1;
193 for(i = 0 ; i < len ; i++)
194 {
195 up = 0;
196 for(j = 0 ; j < T.len ; j++)
197 {
198 temp = a[i] * T.a[j] + ret.a[i + j] + up;
199 if(temp > MAXN)
200 {
201 temp1 = temp - temp / (MAXN + 1) * (MAXN + 1);
202 up = temp / (MAXN + 1);
203 ret.a[i + j] = temp1;
204 }
205 else
206 {
207 up = 0;
208 ret.a[i + j] = temp;
209 }
210 }
211 if(up != 0)
212 ret.a[i + j] = up;
213 }
214 ret.len = i + j;
215 while(ret.a[ret.len - 1] == 0 && ret.len > 1)
216 ret.len--;
217 return ret;
218 }
219 BigNum BigNum::operator/(const int & b) const //大数对一个整数进行相除运算
220 {
221 BigNum ret;
222 int i,down = 0;
223 for(i = len - 1 ; i >= 0 ; i--)
224 {
225 ret.a[i] = (a[i] + down * (MAXN + 1)) / b;
226 down = a[i] + down * (MAXN + 1) - ret.a[i] * b;
227 }
228 ret.len = len;
229 while(ret.a[ret.len - 1] == 0 && ret.len > 1)
230 ret.len--;
231 return ret;
232 }
233 int BigNum::operator %(const int & b) const //大数对一个int类型的变量进行取模运算
234 {
235 int i,d=0;
236 for (i = len-1; i>=0; i--)
237 {
238 d = ((d * (MAXN+1))% b + a[i])% b;
239 }
240 return d;
241 }
242 BigNum BigNum::operator^(const int & n) const //大数的n次方运算
243 {
244 BigNum t,ret(1);
245 int i;
246 if(n<0)
247 exit(-1);
248 if(n==0)
249 return 1;
250 if(n==1)
251 return *this;
252 int m=n;
253 while(m>1)
254 {
255 t=*this;
256 for( i=1;i<<1<=m;i<<=1)
257 {
258 t=t*t;
259 }
260 m-=i;
261 ret=ret*t;
262 if(m==1)
263 ret=ret*(*this);
264 }
265 return ret;
266 }
267 bool BigNum::operator>(const BigNum & T) const //大数和另一个大数的大小比较
268 {
269 int ln;
270 if(len > T.len)
271 return true;
272 else if(len == T.len)
273 {
274 ln = len - 1;
275 while(a[ln] == T.a[ln] && ln >= 0)
276 ln--;
277 if(ln >= 0 && a[ln] > T.a[ln])
278 return true;
279 else
280 return false;
281 }
282 else
283 return false;
284 }
285 bool BigNum::operator >(const int & t) const //大数和一个int类型的变量的大小比较
286 {
287 BigNum b(t);
288 return *this>b;
289 }
290
291 void BigNum::print() //输出大数
292 {
293 int i;
294 cout << a[len - 1];
295 for(i = len - 2 ; i >= 0 ; i--)
296 {
297 cout.width(DLEN);
298 cout.fill('0');
299 cout << a[i];
300 }
301 cout << endl;
302 }
303
304 char* BigNum::tochar(char *ss)
305 {
306 int indexx = 0;
307 for(int i=0; i<len-1; i++)
308 {
309 int tmp = a[i], tmp_len = 0;
310 while(tmp>0)
311 {
312 ss[indexx++] = tmp % 10 + '0';
313 tmp_len++;
314 tmp /= 10;
315 }
316 for(int i=tmp_len+1;i<=4; i++)
317 ss[indexx++] = '0';
318 }
319 int last = a[len-1];
320 while(last>0)
321 {
322 ss[indexx++] = last % 10 + '0';
323 last /= 10;
324 }
325 ss[indexx] = '\0';
326 return ss;
327 }
2 #include<string>
3 #include <cstring>
4 #include<iomanip>
5 #include<algorithm>
6 using namespace std;
7
8 #define MAXN 9999
9 #define MAXSIZE 10
10 #define DLEN 4
11
12 class BigNum
13 {
14 private:
15 int a[500]; //可以控制大数的位数
16 int len; //大数长度
17 public:
18 BigNum(){ len = 1;memset(a,0,sizeof(a)); } //构造函数
19 BigNum(const int); //将一个int类型的变量转化为大数
20 BigNum(const char*); //将一个字符串类型的变量转化为大数
21 BigNum(const BigNum &); //拷贝构造函数
22 BigNum &operator=(const BigNum &); //重载赋值运算符,大数之间进行赋值运算
23
24 friend istream& operator>>(istream&, BigNum&); //重载输入运算符
25 friend ostream& operator<<(ostream&, BigNum&); //重载输出运算符
26
27 BigNum operator+(const BigNum &) const; //重载加法运算符,两个大数之间的相加运算
28 BigNum operator-(const BigNum &) const; //重载减法运算符,两个大数之间的相减运算
29 BigNum operator*(const BigNum &) const; //重载乘法运算符,两个大数之间的相乘运算
30 BigNum operator/(const int &) const; //重载除法运算符,大数对一个整数进行相除运算
31
32 BigNum operator^(const int &) const; //大数的n次方运算
33 int operator%(const int &) const; //大数对一个int类型的变量进行取模运算
34 bool operator>(const BigNum & T)const; //大数和另一个大数的大小比较
35 bool operator>(const int & t)const; //大数和一个int类型的变量的大小比较
36
37 void print(); //输出大数
38 char* tochar(char*);
39 };
40 BigNum::BigNum(const int b) //将一个int类型的变量转化为大数
41 {
42 int c,d = b;
43 len = 0;
44 memset(a,0,sizeof(a));
45 while(d > MAXN)
46 {
47 c = d - (d / (MAXN + 1)) * (MAXN + 1);
48 d = d / (MAXN + 1);
49 a[len++] = c;
50 }
51 a[len++] = d;
52 }
53 BigNum::BigNum(const char*s) //将一个字符串类型的变量转化为大数
54 {
55 int t,k,index,l,i;
56 memset(a,0,sizeof(a));
57 l=strlen(s);
58 len=l/DLEN;
59 if(l%DLEN)
60 len++;
61 index=0;
62 for(i=l-1;i>=0;i-=DLEN)
63 {
64 t=0;
65 k=i-DLEN+1;
66 if(k<0)
67 k=0;
68 for(int j=k;j<=i;j++)
69 t=t*10+s[j]-'0';
70 a[index++]=t;
71 }
72 }
73 BigNum::BigNum(const BigNum & T) : len(T.len) //拷贝构造函数
74 {
75 int i;
76 memset(a,0,sizeof(a));
77 for(i = 0 ; i < len ; i++)
78 a[i] = T.a[i];
79 }
80 BigNum & BigNum::operator=(const BigNum & n) //重载赋值运算符,大数之间进行赋值运算
81 {
82 int i;
83 len = n.len;
84 memset(a,0,sizeof(a));
85 for(i = 0 ; i < len ; i++)
86 a[i] = n.a[i];
87 return *this;
88 }
89 istream& operator>>(istream & in, BigNum & b) //重载输入运算符
90 {
91 char ch[MAXSIZE*4];
92 int i = -1;
93 in>>ch;
94 int l=strlen(ch);
95 int count=0,sum=0;
96 for(i=l-1;i>=0;)
97 {
98 sum = 0;
99 int t=1;
100 for(int j=0;j<4&&i>=0;j++,i--,t*=10)
101 {
102 sum+=(ch[i]-'0')*t;
103 }
104 b.a[count]=sum;
105 count++;
106 }
107 b.len =count++;
108 return in;
109
110 }
111 ostream& operator<<(ostream& out, BigNum& b) //重载输出运算符
112 {
113 int i;
114 cout << b.a[b.len - 1];
115 for(i = b.len - 2 ; i >= 0 ; i--)
116 {
117 cout.width(DLEN);
118 cout.fill('0');
119 cout << b.a[i];
120 }
121 return out;
122 }
123
124 BigNum BigNum::operator+(const BigNum & T) const //两个大数之间的相加运算
125 {
126 BigNum t(*this);
127 int i,big; //位数
128 big = T.len > len ? T.len : len;
129 for(i = 0 ; i < big ; i++)
130 {
131 t.a[i] +=T.a[i];
132 if(t.a[i] > MAXN)
133 {
134 t.a[i + 1]++;
135 t.a[i] -=MAXN+1;
136 }
137 }
138 if(t.a[big] != 0)
139 t.len = big + 1;
140 else
141 t.len = big;
142 return t;
143 }
144 BigNum BigNum::operator-(const BigNum & T) const //两个大数之间的相减运算
145 {
146 int i,j,big;
147 bool flag;
148 BigNum t1,t2;
149 if(*this>T)
150 {
151 t1=*this;
152 t2=T;
153 flag=0;
154 }
155 else
156 {
157 t1=T;
158 t2=*this;
159 flag=1;
160 }
161 big=t1.len;
162 for(i = 0 ; i < big ; i++)
163 {
164 if(t1.a[i] < t2.a[i])
165 {
166 j = i + 1;
167 while(t1.a[j] == 0)
168 j++;
169 t1.a[j--]--;
170 while(j > i)
171 t1.a[j--] += MAXN;
172 t1.a[i] += MAXN + 1 - t2.a[i];
173 }
174 else
175 t1.a[i] -= t2.a[i];
176 }
177 t1.len = big;
178 while(t1.a[len - 1] == 0 && t1.len > 1)
179 {
180 t1.len--;
181 big--;
182 }
183 if(flag)
184 t1.a[big-1]=0-t1.a[big-1];
185 return t1;
186 }
187
188 BigNum BigNum::operator*(const BigNum & T) const //两个大数之间的相乘运算
189 {
190 BigNum ret;
191 int i,j,up;
192 int temp,temp1;
193 for(i = 0 ; i < len ; i++)
194 {
195 up = 0;
196 for(j = 0 ; j < T.len ; j++)
197 {
198 temp = a[i] * T.a[j] + ret.a[i + j] + up;
199 if(temp > MAXN)
200 {
201 temp1 = temp - temp / (MAXN + 1) * (MAXN + 1);
202 up = temp / (MAXN + 1);
203 ret.a[i + j] = temp1;
204 }
205 else
206 {
207 up = 0;
208 ret.a[i + j] = temp;
209 }
210 }
211 if(up != 0)
212 ret.a[i + j] = up;
213 }
214 ret.len = i + j;
215 while(ret.a[ret.len - 1] == 0 && ret.len > 1)
216 ret.len--;
217 return ret;
218 }
219 BigNum BigNum::operator/(const int & b) const //大数对一个整数进行相除运算
220 {
221 BigNum ret;
222 int i,down = 0;
223 for(i = len - 1 ; i >= 0 ; i--)
224 {
225 ret.a[i] = (a[i] + down * (MAXN + 1)) / b;
226 down = a[i] + down * (MAXN + 1) - ret.a[i] * b;
227 }
228 ret.len = len;
229 while(ret.a[ret.len - 1] == 0 && ret.len > 1)
230 ret.len--;
231 return ret;
232 }
233 int BigNum::operator %(const int & b) const //大数对一个int类型的变量进行取模运算
234 {
235 int i,d=0;
236 for (i = len-1; i>=0; i--)
237 {
238 d = ((d * (MAXN+1))% b + a[i])% b;
239 }
240 return d;
241 }
242 BigNum BigNum::operator^(const int & n) const //大数的n次方运算
243 {
244 BigNum t,ret(1);
245 int i;
246 if(n<0)
247 exit(-1);
248 if(n==0)
249 return 1;
250 if(n==1)
251 return *this;
252 int m=n;
253 while(m>1)
254 {
255 t=*this;
256 for( i=1;i<<1<=m;i<<=1)
257 {
258 t=t*t;
259 }
260 m-=i;
261 ret=ret*t;
262 if(m==1)
263 ret=ret*(*this);
264 }
265 return ret;
266 }
267 bool BigNum::operator>(const BigNum & T) const //大数和另一个大数的大小比较
268 {
269 int ln;
270 if(len > T.len)
271 return true;
272 else if(len == T.len)
273 {
274 ln = len - 1;
275 while(a[ln] == T.a[ln] && ln >= 0)
276 ln--;
277 if(ln >= 0 && a[ln] > T.a[ln])
278 return true;
279 else
280 return false;
281 }
282 else
283 return false;
284 }
285 bool BigNum::operator >(const int & t) const //大数和一个int类型的变量的大小比较
286 {
287 BigNum b(t);
288 return *this>b;
289 }
290
291 void BigNum::print() //输出大数
292 {
293 int i;
294 cout << a[len - 1];
295 for(i = len - 2 ; i >= 0 ; i--)
296 {
297 cout.width(DLEN);
298 cout.fill('0');
299 cout << a[i];
300 }
301 cout << endl;
302 }
303
304 char* BigNum::tochar(char *ss)
305 {
306 int indexx = 0;
307 for(int i=0; i<len-1; i++)
308 {
309 int tmp = a[i], tmp_len = 0;
310 while(tmp>0)
311 {
312 ss[indexx++] = tmp % 10 + '0';
313 tmp_len++;
314 tmp /= 10;
315 }
316 for(int i=tmp_len+1;i<=4; i++)
317 ss[indexx++] = '0';
318 }
319 int last = a[len-1];
320 while(last>0)
321 {
322 ss[indexx++] = last % 10 + '0';
323 last /= 10;
324 }
325 ss[indexx] = '\0';
326 return ss;
327 }
1 int main(void)
2 {
3 char str[10],s[10],*ans;
4 ans = (char*)calloc(2000,sizeof(char));
5 int len_str, len_s, point, mi, len_ans;
6 while(cin >> str >> mi)
7 {
8 //=======
9 //cout << "str: " << str << endl;
10 point = -1, len_s = 0;
11 len_str = strlen(str);
12 int prezero = 0;//前缀0
13 while(str[prezero]=='0')
14 prezero++;
15 for(int i=prezero; i<len_str; i++)
16 {
17 if(str[i]=='.')
18 {
19 point = len_str - i - 1;
20 continue;
21 }
22 s[len_s++] = str[i];
23 }
24 s[len_s] = '\0';
25 //=======
26 //cout << "s" << s << "}" << endl;
27 if(point==-1)
28 point = 0;
29 else
30 {
31 while(str[len_str-1]=='0')
32 {
33 point--;
34 len_s--;
35 len_str--;
36 }
37 s[len_s] = '\0';
38 }
39 //=======
40 // cout << "s" << s << "}" << endl;
41 int zero_test = 0;
42 for(int i=0; i<len_s; i++)
43 zero_test += s[i]-'0';
44 if(zero_test==0)
45 cout << "0" << endl;
46 else{
47 point*=mi;
48 //cout << "point:" << point << endl;
49 BigNum b(s);
50 b = b^mi;
51 ans = b.tochar(ans);
52 //=======
53 //cout << "ans: " << ans << endl;
54 len_ans = strlen(ans);
55 if(point >= len_ans)
56 {
57 cout << "." << string(point-len_ans, '0');
58 for(int i=len_ans-1; i>=0; i--)
59 cout << ans[i];
60 }
61 else
62 {
63 for(int i=len_ans-1; i>point-1; i--)
64 cout << ans[i];
65 if(point>0)
66 cout << ".";
67 for(int i=point-1; i>=0; i--)
68 cout << ans[i];
69 }
70 cout << endl;
71 }
72 }
73 return 0;
74 }
2 {
3 char str[10],s[10],*ans;
4 ans = (char*)calloc(2000,sizeof(char));
5 int len_str, len_s, point, mi, len_ans;
6 while(cin >> str >> mi)
7 {
8 //=======
9 //cout << "str: " << str << endl;
10 point = -1, len_s = 0;
11 len_str = strlen(str);
12 int prezero = 0;//前缀0
13 while(str[prezero]=='0')
14 prezero++;
15 for(int i=prezero; i<len_str; i++)
16 {
17 if(str[i]=='.')
18 {
19 point = len_str - i - 1;
20 continue;
21 }
22 s[len_s++] = str[i];
23 }
24 s[len_s] = '\0';
25 //=======
26 //cout << "s" << s << "}" << endl;
27 if(point==-1)
28 point = 0;
29 else
30 {
31 while(str[len_str-1]=='0')
32 {
33 point--;
34 len_s--;
35 len_str--;
36 }
37 s[len_s] = '\0';
38 }
39 //=======
40 // cout << "s" << s << "}" << endl;
41 int zero_test = 0;
42 for(int i=0; i<len_s; i++)
43 zero_test += s[i]-'0';
44 if(zero_test==0)
45 cout << "0" << endl;
46 else{
47 point*=mi;
48 //cout << "point:" << point << endl;
49 BigNum b(s);
50 b = b^mi;
51 ans = b.tochar(ans);
52 //=======
53 //cout << "ans: " << ans << endl;
54 len_ans = strlen(ans);
55 if(point >= len_ans)
56 {
57 cout << "." << string(point-len_ans, '0');
58 for(int i=len_ans-1; i>=0; i--)
59 cout << ans[i];
60 }
61 else
62 {
63 for(int i=len_ans-1; i>point-1; i--)
64 cout << ans[i];
65 if(point>0)
66 cout << ".";
67 for(int i=point-1; i>=0; i--)
68 cout << ans[i];
69 }
70 cout << endl;
71 }
72 }
73 return 0;
74 }