高精度/sgu112 a^b - b^a
题意
给出一个a一个b,求a^b - b^a
分析
高精度不解释
需要写三个高精度:
mult-计算乘法
compare-比较两个数的大小
minus-计算减法
Accepted Code
1 { 2 PROBLEM:SGU112 3 AUTHER:Rinyo 4 MEMO:高精度 模拟 5 } 6 Program sgu112; 7 Type 8 Rec = REcord 9 len:Longint; 10 a:Array[0..1130]Of Longint; 11 End; 12 Var 13 a,b,aa,bb,i:Longint; 14 flag:Boolean; 15 aaa,bbb:REc; 16 Procedure mult(var a:Rec;k:Longint); 17 Var 18 i:Longint; 19 Begin 20 For i:=1 To a.len Do a.a[i]:=a.a[i]*k; 21 For i:=1 To a.len-1 Do Begin 22 Inc(a.a[i+1],a.a[i] Div 10); 23 a.a[i]:=a.a[i] Mod 10; 24 End; 25 i:=a.len; 26 While a.a[i]>9 Do Begin 27 Inc(a.a[i+1],a.a[i] Div 10); 28 a.a[i]:=a.a[i] Mod 10; 29 Inc(i); 30 End; 31 a.len:=i; 32 End; 33 Function compare(a,b:REc):Boolean; 34 Var 35 i:Longint; 36 Begin 37 If a.len<>b.len Then Begin 38 compare:=a.len>b.len; 39 Exit; 40 End Else 41 Begin 42 compare:=true; 43 For i:=a.len Downto 1 Do 44 If a.a[i]<>b.a[i] Then Begin 45 compare:=a.a[i]>b.a[i]; 46 Exit; 47 End; 48 End; 49 End; 50 51 Function minus(a,b:Rec):Rec; 52 Var 53 i:Longint; 54 Begin 55 For i:=1 To a.len Do Begin 56 a.a[i]:=a.a[i]-b.a[i]; 57 If a.a[i]<0 Then Begin 58 Inc(a.a[i],10); 59 Dec(a.a[i+1]); 60 End; 61 End; 62 i:=a.len; 63 While (a.a[i]=0) And (i>1) Do Dec(i); 64 a.len:=i; 65 minus:=a; 66 End; 67 68 Begin 69 ReadLn(a,b); 70 aa:=a;bb:=b; 71 aaa.len:=0;bbb.len:=0; 72 WHile aa>0 Do Begin 73 Inc(aaa.len); 74 aaa.a[aaa.len]:=aa Mod 10; 75 aa:=aa Div 10; 76 End; 77 While bb>0 Do Begin 78 Inc(bbb.len); 79 bbb.a[bbb.len]:=bb Mod 10; 80 bb:=bb Div 10; 81 End; 82 For i:=2 To b Do mult(aaa,a); 83 For i:=2 To a Do mult(bbb,b); 84 flag:=compare(aaa,bbb); 85 If flag Then aaa:=minus(aaa,bbb) 86 Else Begin 87 Write('-'); 88 aaa:=minus(bbb,aaa); 89 End; 90 For i:=aaa.len Downto 1 Do Write(aaa.a[i]); 91 WriteLn; 92 End.