Skip to content

Commit 38ba4c1

Browse files
committed
POJ1001
1 parent 1568475 commit 38ba4c1

File tree

3 files changed

+198
-1
lines changed

3 files changed

+198
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
| 递归和分治法 | - |
2626
| 递推 | - |
2727
| 构造法 | [POJ-3295](/reports/POJ3295-Tautology) [POJ-3239](/reports/POJ3239-Solution%20to%20the%20n%20Queens%20Puzzle) |
28-
| 模拟法 | [POJ-1008](/reports/POJ1008-Maya%20Calendar) [POJ-1068](/reports/POJ1068-Parencodings) [POJ-2632](/reports/POJ2632-Crashing%20Robots) [POJ-1573](/reports/POJ1573-Robot%20Motion) [POJ-2993](/reports/POJ2993-Emag%20eht%20htiw%20Em%20Pleh) [POJ-2996](/reports/POJ2996-Help%20Me%20with%20the%20Game) [POJ-3087](http://exp-blog.com/2018/06/17/pid-541/) |
28+
| 模拟法 | [POJ-1008](/reports/POJ1008-Maya%20Calendar) [POJ-1068](/reports/POJ1068-Parencodings) [POJ-2632](/reports/POJ2632-Crashing%20Robots) [POJ-1573](/reports/POJ1573-Robot%20Motion) [POJ-2993](/reports/POJ2993-Emag%20eht%20htiw%20Em%20Pleh) [POJ-2996](/reports/POJ2996-Help%20Me%20with%20the%20Game) [POJ-3087](/reports/POJ3087-Shuffle'm%20Up) |
2929
| 高精度算法 | [POJ-1001](http://exp-blog.com/2018/06/17/pid-568/) [POJ-1503](http://exp-blog.com/2018/06/17/pid-573/) [POJ-2109](/reports/POJ2109-Power%20of%20Cryptography) [POJ-2389](http://exp-blog.com/2018/06/17/pid-576/) [POJ-2602](http://exp-blog.com/2018/06/17/pid-580/) [POJ-3982](http://exp-blog.com/2018/06/17/pid-584/) |
3030

3131

+193
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
## [[POJ](http://poj.org/)] [[INDEX](https://github.com/lyy289065406/POJ-Solving-Reports)] [1001] [[Exponentiation](http://poj.org/problem?id=1001)]
2+
3+
> [Time: 1000MS] [Memory: 10000K] [难度: 初级] [分类: 高精度算法]
4+
5+
------
6+
7+
## 问题描述
8+
9+
无。
10+
11+
12+
## 解题思路
13+
14+
浮点**大数**求幂,水题一道,把“大数乘浮点数”按指数循环就OK了,注意结果的整数部分若为0,则不保留整数部分。小数部分若为0,则不保留小数部分和小数点。
15+
16+
17+
## AC 源码
18+
19+
20+
```c
21+
//Memory Time
22+
//1232K 0MS
23+
24+
#include<iostream>
25+
#include<string>
26+
using namespace std;
27+
28+
const int size=1000; //大数位数
29+
30+
void mult(char* A,char* B,char* ans)
31+
{
32+
int i,j,k;
33+
34+
int fract; //总小数个数
35+
int dot=-1; //小数点位置
36+
for(k=0;A[k]!='\0';k++)
37+
if(A[k]=='.')
38+
dot=k;
39+
int lena=k;
40+
if(dot==-1)
41+
fract=0;
42+
else
43+
fract=lena-dot-1;
44+
45+
dot=-1;
46+
for(k=0;B[k]!='\0';k++)
47+
if(B[k]=='.')
48+
dot=k;
49+
int lenb=k;
50+
if(dot==-1)
51+
fract+=0;
52+
else
53+
fract+=(lenb-dot-1); //总小数个数
54+
55+
int a[size+1]={0};
56+
int b[size+1]={0};
57+
int pa=0,pb=0;
58+
59+
/*倒序*/
60+
61+
for(i=lena-1;i>=0;i--)
62+
{
63+
if(A[i]=='.')
64+
continue;
65+
a[pa++]=A[i]-'0';
66+
}
67+
for(j=lenb-1;j>=0;j--)
68+
{
69+
if(B[j]=='.') //暂时删除小数点
70+
continue;
71+
b[pb++]=B[j]-'0';
72+
}
73+
74+
int c[2*size+1]={0};
75+
int lenc;
76+
for(pb=0;pb<lenb;pb++)
77+
{
78+
int w=0; //低位到高位的进位
79+
for(pa=0;pa<=lena;pa++) // = 为了处理最后的进位
80+
{
81+
int temp=a[pa]*b[pb]+w;
82+
w=temp/10;
83+
temp=(c[pa+pb]+=temp%10);
84+
c[lenc=pa+pb]=temp%10;
85+
w+=temp/10;
86+
}
87+
}
88+
89+
/*倒序,得到没有小数点的ans*/
90+
91+
for(pa=0,pb=lenc;pb>=0;pb--)
92+
ans[pa++]=c[pb]+'0';
93+
ans[pa]='\0';
94+
lena=pa;
95+
96+
/*插入小数点*/
97+
98+
bool flag=true; //标记是否需要删除小数末尾的0
99+
if(fract==0) //小数位数为0,无需插入小数点
100+
flag=false;
101+
else if(fract<lena) //小数位数小于ans长度,在ans内部插入小数点
102+
{
103+
ans[lena+1]='\0';
104+
for(i=0,pa=lena;pa>0;pa--,i++)
105+
{
106+
if(i==fract)
107+
{
108+
ans[pa]='.';
109+
break;
110+
}
111+
else
112+
ans[pa]=ans[pa-1];
113+
}
114+
115+
}
116+
else //小数位数大于等于ans长度,在ans前面恰当位置插入小数点
117+
{
118+
char temp[size+1];
119+
strcpy(temp,ans);
120+
ans[0]='0';
121+
ans[1]='.';
122+
for(int i=0;i<fract-lena;i++) //补充0
123+
ans[i+2]='0';
124+
for(j=i,pa=0;pa<lena;pa++)
125+
ans[j++]=temp[pa];
126+
ans[j]='\0';
127+
}
128+
129+
/*删除ans小数末尾的0*/
130+
131+
if(flag)
132+
{
133+
lena=strlen(ans);
134+
pa=lena-1;
135+
while(ans[pa]=='0')
136+
ans[pa--]='\0';
137+
if(ans[pa]=='.') //小数全为0
138+
ans[pa--]='\0';
139+
}
140+
141+
/*删除ans整数开头的0,但至少保留1个0*/
142+
143+
pa=0;
144+
while(ans[pa]=='0') //寻找ans开头第一个不为0的位置
145+
pa++;
146+
147+
if(ans[pa]=='\0') //没有小数
148+
{
149+
ans[0]='0';
150+
ans[1]='\0';
151+
}
152+
else //有小数
153+
{
154+
for(i=0;ans[pa]!='\0';i++,pa++)
155+
ans[i]=ans[pa];
156+
ans[i]='\0';
157+
}
158+
return;
159+
}
160+
161+
char a[size+1];
162+
char ans[size*size+1];
163+
164+
int main(void)
165+
{
166+
int b;
167+
while(cin>>a>>b)
168+
{
169+
memset(ans,'\0',sizeof(ans));
170+
ans[0]='1';
171+
ans[3]='\0';
172+
173+
for(int i=1;i<=b;i++)
174+
mult(a,ans,ans);
175+
176+
cout<<ans<<endl;
177+
}
178+
return 0;
179+
}
180+
```
181+
182+
------
183+
184+
## 版权声明
185+
186+
 [![Copyright (C) EXP,2016](https://img.shields.io/badge/Copyright%20(C)-EXP%202016-blue.svg)](http://exp-blog.com) [![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)
187+
188+
189+
- Site: [http://exp-blog.com](http://exp-blog.com)
190+
- Mail: <a href="mailto:[email protected]?subject=[EXP's Github]%20Your%20Question%20(请写下您的疑问)&amp;body=What%20can%20I%20help%20you?%20(需要我提供什么帮助吗?)">[email protected]</a>
191+
192+
193+
------
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copyright (C)
2+
# Author: EXP
3+
# Site : http://exp-blog.com
4+

0 commit comments

Comments
 (0)