高精度计算练习2
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了高精度计算练习2相关的知识,希望对你有一定的参考价值。
高精度运算_乘法
AYYZOJ p1445
1 program p1445; 2 const 3 max=5000; 4 var 5 a,b,c:array[1..max] of word; 6 n1,n2:string; 7 lena,lenb,lenc,i,j,x:integer; 8 begin 9 readln(n1); 10 readln(n2); 11 lena:=length(n1); lenb:=length(n2); 12 for i:=1 to lena do a[lena-i+1]:=ord(n1[i])-ord(‘0‘); 13 for i:=1 to lenb do b[lenb-i+1]:=ord(n2[i])-ord(‘0‘); 14 for i:=1 to lena do 15 begin 16 x:=0; 17 for j:=1 to lenb do 18 begin 19 C[i+j-1]:= A[i]*B[j]+ x + C[i+j-1]; 20 x:= C[i+j-1] div 10; 21 C[i+j-1]:= C[i+j-1] mod 10 22 end; 23 c[i+j]:= x; 24 end; 25 lenc:=i+j; 26 while (c[lenc]=0) and (lenc>1) do dec(lenc); 27 for i:=lenc downto 1 do write(c[i]); 28 end.
高精度运算_A/B问题1
AYYZOJ p1446
1 program p1446; 2 const e=20; 3 var 4 a,d,x:array[0..e] of integer; 5 n,m,t:integer; 6 begin 7 read(n,m); 8 write(n,‘/‘,m,‘=‘); 9 a[0]:=n; d[0]:=n div m; 10 x[0]:=n mod m; 11 write(d[0],‘.‘); 12 for t:=1 to e do 13 begin 14 //if x[t-1]=0 then exit; {去掉也能A} 15 a[t]:=x[t-1]*10; 16 d[t]:=a[t] div m; 17 write(d[t]); 18 x[t]:=a[t] mod m; 19 end; 20 end.
高精度运算_A/B问题2
AYYZOJ p1447
1 const max=200;dot=100; 2 var a,c:array[0..max] of byte; 3 x,n2:longint; n1:string; 4 lena:integer; code,i,j:integer; 5 begin 6 readln(n1); 7 readln(n2); 8 lena:=length(n1); 9 for i:=1 to lena do a[i] := ord(n1[i]) - ord(‘0‘); 10 x:=0; 11 for i:=1 to lena do begin 12 c[i]:=(x*10+a[i]) div n2; 13 x:=(x*10+a[i]) mod n2; 14 end; 15 j:=1; 16 while (c[j]=0) and (j<lena) do inc(j); 17 if j<>1 then 18 for i:=j to lena do begin c[i-j+1]:=c[i];c[i]:=0;end; 19 c[0]:=lena-j+1; 20 if x=0 then begin 21 for i:=1 to c[0] do write(c[i]); 22 writeln;halt; 23 end; 24 j:=c[0]+dot; 25 for i:=1 to dot do begin 26 c[c[0]+i]:=x*10 div n2; 27 x:=x*10 mod n2; 28 if x=0 then begin j:=c[0]+i;break;end; 29 end; 30 for i:=1 to j do 31 if i=c[0] then write(c[i],‘.‘) 32 else write(c[i]); 33 writeln 34 end.
高精度运算_3的n次幂
AYYZOJ p1450
1 program p1450; 2 var 3 a:array[1..500] of byte; 4 i,j,c,n,l:integer; 5 begin 6 readln(n); 7 a[1]:=1;l:=1; 8 for i:=1 to n do 9 begin 10 c:=0; 11 for j:=1 to l do 12 begin 13 a[j]:=a[j]*3+c; 14 c:=a[j] div 10; 15 a[j]:=a[j] mod 10; 16 end; 17 if c>0 then begin inc(l);a[l]:=c;end; 18 end; 19 for i:=l downto 1 do write(a[i]); 20 writeln; 21 end.
高精度运算_求s=1+2+...+n
AYYZOJ p1451
1 var 2 n:longint; 3 a,b,c:array[1..100] of integer; 4 la,lb,lc,k,i,j:integer; 5 procedure mul(x,y:longint); 6 begin 7 la:=0; 8 while x>0 do 9 begin 10 inc(la); 11 a[la]:=x mod 10; 12 x:=x div 10; 13 end; 14 lb:=0; 15 while y>0 do 16 begin 17 inc(lb); 18 b[lb]:=y mod 10; 19 y:=y div 10; 20 end; 21 for i:=1 to la do 22 begin 23 k:=0; 24 for j:=1 to lb do 25 begin 26 c[i+j-1]:=a[i]*b[j]+k+c[i+j-1]; 27 k:=c[i+j-1] div 10; 28 c[i+j-1]:=c[i+j-1] mod 10; 29 end; 30 c[i+j]:=k; 31 end; 32 lc:=i+j; 33 while(c[lc]=0) and (lc>1) do dec(lc); 34 for i:=lc downto 1 do write(c[i]);writeln; 35 end; 36 begin 37 readln(n); 38 if n mod 2=0 then mul(n div 2,n+1) {等差数列求和公式} 39 else mul(n,(n+1) div 2); 40 end.
高精度运算_求n的阶乘
AYYZOJ p1452
1 const maxn=3001;maxm=10000; 2 type arr=array[1..maxm] of integer; 3 var 4 ans:arr; 5 p,num:array[1..maxn] of integer; 6 n,i,j,k:longint; 7 flag:boolean;la:integer; 8 procedure add(x:longint); 9 var i:longint; 10 begin 11 i:=1; 12 while p[i]<=x do begin 13 while (x mod p[i]=0) do 14 begin 15 inc(num[i],1); 16 x:=x div p[i]; 17 end; 18 inc(i); 19 end; 20 end; 21 procedure mul(var a:arr;c:longint;var la:integer); 22 var i:longint; 23 begin 24 a[1]:=a[1]*c; 25 for i:=2 to la do 26 begin 27 a[i]:=a[i]*c; 28 a[i]:=a[i]+a[i-1] div 10; 29 a[i-1]:=a[i-1] mod 10; 30 end; 31 while a[la]>=10 do 32 begin 33 inc(la); 34 a[la]:=a[la-1] div 10; 35 a[la-1]:=a[la-1] mod 10; 36 end; 37 end; 38 begin 39 p[1]:=2;k:=1; 40 for i:=3 to maxn do begin 41 flag:=false; 42 for j:=2 to trunc(sqrt(i)) do 43 if i mod j=0 then begin flag:=true;break;end; 44 if not flag then begin inc(k);p[k]:=i;end; 45 end; 46 fillchar(ans,sizeof(ans),0); 47 fillchar(num,sizeof(num),0); 48 readln(n); 49 for i:=2 to n do add(i); 50 ans[1]:=1; la:=1; 51 for i:=1 to maxn do 52 if p[i]>n then break else for j:=1 to num[i] do mul(ans,p[i],la); 53 for i:=la downto 1 do write(ans[i]);writeln; 54 end.
N!
1 const m=10000000; 2 var a:array[1..m] of integer; 3 f:real; 4 y,c,i,j,n,k,l,t:longint; 5 st1,st2:string; 6 begin 7 write(‘input a number n:‘); 8 read(n); 9 // for i:=1 to n do 10 // f:=f+ln(i)/ln(10); 11 // t:=trunc(f)+1; {计算可能需要的位数} 12 a[1]:=1;l:=1;k:=1; 13 for i:=1 to n do 14 begin 15 c:=0; 16 while a[k]=0 do k:=k+1; 17 for j:=k to l do 18 begin 19 y:=a[j]*i+c; {该位上的数乘以I加上低位的进位} 20 c:=y div 10000; {每四位数占用1个数组单元,c是进位} 21 a[j]:=y mod 10000 22 end; 23 if c>0 then begin 24 l:=l+1; 25 a[l]:=c; {若有进位则数组的下标指针加1,增加一} 26 end; {个存储单元} 27 end; 28 write(n,‘!=‘); 29 for i:=l downto 1 do {输出处理} 30 begin 31 if i=l then write(a[i]) 32 else begin 33 str(a[i],st1); 34 while length(st1)<4 do 35 st1:=‘0‘+st1; 36 write(st1); 37 end; 38 end; 39 writeln; 40 end.
以上是关于高精度计算练习2的主要内容,如果未能解决你的问题,请参考以下文章