pku1001 Exponentiation

一道纯的高精度题,就是靠基础代码了,1AC0ms无压力。

注意整数和小数的分界,还有0的时候。

View Code
 1 program pku1001(input,output);
2 type
3 number = array[0..100] of integer;
4 var
5 power : integer;
6 s,s1,s2 : ansistring;
7 x,answer : number;
8 n : longint;
9 procedure init;
10 begin
11 readln(s);
12 s1:=copy(s,1,pos(' ',s)-1);
13 delete(s,1,pos(' ',s));
14 val(s,power);
15 n:=length(s1)-pos('.',s1);
16 delete(s1,pos('.',s1),1);
17 n:=n*power;
18 while s1[1]='0' do
19 delete(s1,1,1);
20 end; { init }
21 procedure change();
22 var
23 i : longint;
24 begin
25 x[0]:=length(s1);
26 for i:=1 to length(s1) do
27 x[i]:=ord(s1[length(s1)+1-i])-48;
28 answer:=x;
29 end; { change }
30 function multiply(x,y :number ):number;
31 var
32 i,j : longint;
33 begin
34 fillchar(multiply,sizeof(multiply),0);
35 multiply[0]:=x[0]+y[0]-1;
36 for i:=1 to x[0] do
37 for j:=1 to y[0] do
38 begin
39 inc(multiply[i+j-1],x[i]*y[j]);
40 inc(multiply[i+j],multiply[i+j-1] div 10);
41 multiply[i+j-1]:=multiply[i+j-1] mod 10;
42 end;
43 if multiply[multiply[0]+1]>0 then
44 inc(multiply[0]);
45 end; { multiply }
46 procedure main;
47 var
48 i : longint;
49 begin
50 for i:=1 to power-1 do
51 answer:=multiply(answer,x);
52 s1:='';
53 s2:='';
54 for i:=answer[0] downto n+1 do
55 s1:=s1+chr(answer[i]+48);
56 for i:=n downto 1 do
57 s2:=s2+chr(answer[i]+48);
58 s:=s1+'.'+s2;
59 while s[1]='0' do
60 delete(s,1,1);
61 while s[length(s)]='0' do
62 delete(s,length(s),1);
63 if s[length(s)]='.' then
64 delete(s,length(s),1);
65 end; { main }
66 procedure print;
67 begin
68 if s='' then
69 writeln(0)
70 else
71 writeln(s);
72 end; { print }
73 begin
74 while not eof do
75 begin
76 init;
77 change;
78 main;
79 print;
80 end;
81 end.



posted @ 2012-04-05 15:22  Codinginging  阅读(184)  评论(0编辑  收藏  举报