導航:首頁 > 源碼編譯 > 高精度乘法演算法

高精度乘法演算法

發布時間:2022-12-22 08:00:36

㈠ 高精度乘法。輸入兩個正整數,求它們的積。 中間標注的地方,想了好長時間還很模糊,有沒有簡單的理解。

(1)為什麼要減48?
因為輸入的a1,b1是字元串,字元串中的0-9是ascii編碼,其中0的ascii編碼是48,1是49,以此類推,因此需要減48變成真正的十進制數。
(2)計算過程
程序中模擬的是小學生列豎式計算多位數乘法的過程,即乘數由低位到高位和被乘數每一位相乘,進位累加到高一位(x存儲井位數)。
但是程序把最後的累加過程一步完成了,直接保存在c數組中。

㈡ 3.兩個數相乘,小數點後位數沒有限制,請寫一個高精度演算法 與大數相乘原理一樣,只是調整小數點位

小數是我國最早提出和使用的。早在3世紀,我國古代數學家劉徽在解決一個數學問題時就提出把整數個位以下無法標出名稱的部分稱為徽數。
小數的名稱是13世紀我國元代數學家朱世傑提出的。在13世紀中葉我國出現了低一格表示小數的記法.
在西方,小數出現很晚,直到16世紀,法國數學家克拉維斯首先使用了小數作為整數部分與小數部分分界的記號。

雖然我國對小數的認識遠遠早於歐洲,但現代數學中所使用的小數的表示法卻是從歐洲傳入我國的。歐洲關於十進小數的最大貢獻者是荷蘭工程師斯蒂文(Simon Stevin,1548?1620)。他從製造利息表中體會到十進小數的優越性,因此他竭力主張把十進小數引進到整個算術運算中去,使十進小數有效地參與記數。不過,斯蒂文的小數記法並不高明,如139.654,他寫作135⊙6①5②4③,每個數後面圈中的數是用來指明它前面數字位置的,這種表示方法,使小數的形式復雜化,並且給小數的運算帶來很大的麻煩。1592年,瑞士數學家布爾基(Jobst Burgi)對此作出較大的改進。他用一空心小圓圈把整數部分和小數部分隔開,比如把36.548表示為36。548,這與現代的表示法已極為接近。大約過了一年,德國的克拉維斯,首先用黑點代替了小圓圈。他在1608年發表的《代數學》中,將他的這一做法公之於世,至此,小數的現代記法才被確立下來。追問我要被老師罵了、做得太簡單了、而且文字太少........... 回答再幫你添加一點內容吧,這兩個素材你可以適當的做一些整理。

小數點,數學符號,用於在十進制中隔開整數部分和小數部分。
小數點的由來
中國自古以來就使用十進位制計數法,一些實用的計量單位也採用十進制,所以很容易產生十進分數,即小數的概念。第一個將這一概念用文字表達出來的是魏晉時代的劉徽。他在計算圓周率的過程中,用到尺、寸、分、厘、毫、秒 、忽等7個單位;對於忽以下的更小單位則不再命名,而統稱為「微數」。
到了宋、元時代,小數概念得到了進一步的普及和更明確的表示。楊輝《日用演算法》(1262年)載有兩斤換算 的口訣:「一求,隔位六二五;二求,退位一二五」,即1/16=0�0625;2/16=0�125。 這里的「隔位」、「退位」已含有指示小數點位置的意義。秦九韶則將單位注在表示整數部分個位的籌碼之下,例如: —Ⅲ—Ⅱ表示13.12寸 寸是世界上最早的小數表示法。
在歐洲和伊斯蘭國家,古巴比倫的六十進制長期以來居於統治地位,一些經典科學著作都是採用六十進制,因此十進制小數的概念遲遲沒有發展起來。15世紀中亞地區的阿爾卡西(?~1429)是中國以外第一個應用小數的人。歐洲數學家直到16世紀才開始考慮小數,其中較突出的是荷蘭人斯蒂文(1548~1620),他在《論十進制》(1583年)一書中明確表示法。例如把5.714記為:5◎7①1②4③或5,7'1''4'''。而第一個把小數表示成今日世界通用的形式的人是德國數學家克拉維斯(1537~1612),他在《星盤》(1593年)一書中開始使用小數點作為整數部分與小數部分之間的分界符。
而中國比歐洲早採用了三百多年。
小數點盡管小,但是作用極大。我們時刻都不可忽略這個小小的符號。因為這個不起眼的差錯,人類釀過一個又一個悲劇。正可謂「差之毫釐,謬以千里」。1967年,前蘇聯「聯盟一號」墜毀事件,造成了不可挽回的損失。直接原因是在地面檢查時,忽略了一個小數點……導致了數億元財富的損失,人類還失去了一位太空英雄----科馬洛夫的生命! 追問雖然你的內容不錯,我也看得懂,可是只能當天做啊......我都交了...算了吧

㈢ 高精度計算的單精度乘法

單精度乘法是計算范圍次於高精度乘法的一種運算,只是運算效率略比高精度計算低。
單精度乘法過程樣例:
const
maxcount=進制位
maxlen=記錄高精度數組大小
procere mulnum(a:bignum;x:longint;,var c:bignum);
var
i:longint;
begin
fillchar(c,sizeof(c),0);c[0]:=a[0];
for i:=1 to c[0] do c[i]:=a[i]*x;
for i:=1 to c[0] do {進位}
begin
inc(c[i+1],c[i] div maxcount);
c[i]:=c[i] mod 10;
end;
while c[c[0]+1]>0 do
begin
inc(c[0]);
inc(c[c[0]+1],c[c[0]] div maxcount);
c[c[0]]:=c[c[0]] mod maxcount;
end;
end;

㈣ 高精度乘法,高精度是什麼意思,據說可以一位存好幾位

到目前為止,我們這個星球上的任何數字電路中,任何1位的寄存器電路只能存1位數,就是0 或 1,絕對不可能存好幾位的啦,高精度是指可以表示的小數點後面的位數比較多,高精度乘法一般是指 浮點乘法,在實際運用中,小數表達一般用的比較少的.只有在交流電機或變頻電源里用的比較多.

㈤ Pascal高精度計算,加減乘除

2.1高精度加法

高精度加法程序如下:

program HighPrecision1_Plus;
const
fn_inp='hp1.inp';
fn_out='hp1.out';
maxlen=100; { max length of the number }
type
hp=record
len:integer; { length of the number }
s:array[1..maxlen] of integer
{ s[1] is the lowest position
s[len] is the highest position }
end;
var
x:array[1..2] of hp;
y:hp; { x:input ; y:output }

procere PrintHP(const p:hp);
var i:integer;
begin
for i:=p.len downto 1 do write(p.s[i]);
end;

procere init;
var
st:string;
j,i:integer;
begin
assign(input,fn_inp);
reset(input);
for j:=1 to 2 do
begin
readln(st);
x[j].len:=length(st);
for i:=1 to x[j].len do { change string to HP }
x[j].s[i]:=ord(st[x[j].len+1-i])-ord('0');
end;
close(input);
end;

procere Plus(a,b:hp;var c:hp); { c:=a+b }
var i,len:integer;
begin
fillchar(c,sizeof(c),0);
if a.len>b.len then len:=a.len { get the bigger length of a,b }
else len:=b.len;
for i:=1 to len do { plus from low to high }
begin
inc(c.s[i],a.s[i]+b.s[i]);
if c.s[i]>=10 then
begin
dec(c.s[i],10);
inc(c.s[i+1]); { add 1 to a higher position }
end;
end;
if c.s[len+1]>0 then inc(len);
c.len:=len;
end;

procere main;
begin
Plus(x[1],x[2],y);
end;

procere out;
begin
assign(output,fn_out);
rewrite(output);
PrintHP(y);
writeln;
close(output);
end;

begin
init;
main;
out;
end.

2. 2 高精度減法

高精度減法程序如下:

program HighPrecision2_Subtract;
const
fn_inp='hp2.inp';
fn_out='hp2.out';
maxlen=100; { max length of the number }
type
hp=record
len:integer; { length of the number }
s:array[1..maxlen] of integer
{ s[1] is the lowest position
s[len] is the highest position }
end;
var
x:array[1..2] of hp;
y:hp; { x:input ; y:output }
positive:boolean;

procere PrintHP(const p:hp);
var i:integer;
begin
for i:=p.len downto 1 do write(p.s[i]);
end;

procere init;
var
st:string;
j,i:integer;
begin
assign(input,fn_inp);
reset(input);
for j:=1 to 2 do
begin
readln(st);
x[j].len:=length(st);
for i:=1 to x[j].len do { change string to HP }
x[j].s[i]:=ord(st[x[j].len+1-i])-ord('0');
end;
close(input);
end;

procere Subtract(a,b:hp;var c:hp); { c:=a-b, suppose a>=b }
var i,len:integer;
begin
fillchar(c,sizeof(c),0);
if a.len>b.len then len:=a.len { get the bigger length of a,b }
else len:=b.len;
for i:=1 to len do { subtract from low to high }
begin
inc(c.s[i],a.s[i]-b.s[i]);
if c.s[i]<0 then
begin
inc(c.s[i],10);
dec(c.s[i+1]); { add 1 to a higher position }
end;
end;
while(len>1) and (c.s[len]=0) do dec(len);
c.len:=len;
end;

function Compare(const a,b:hp):integer;
{
1 if a>b
0 if a=b
-1 if a<b
}
var len:integer;
begin
if a.len>b.len then len:=a.len { get the bigger length of a,b }
else len:=b.len;
while(len>0) and (a.s[len]=b.s[len]) do dec(len);
{ find a position which have a different digit }
if len=0 then compare:=0 { no difference }
else compare:=a.s[len]-b.s[len];
end;

procere main;
begin
if Compare(x[1],x[2])<0 then positive:=false
else positive:=true;
if positive then Subtract(x[1],x[2],y)
else Subtract(x[2],x[1],y);
end;

procere out;
begin
assign(output,fn_out);
rewrite(output);
if not positive then write('-');
PrintHP(y);
writeln;
close(output);
end;

begin
init;
main;
out;
end.

2.3高精度乘高精度
程序如下:
program HighPrecision4_Multiply2;
const
fn_inp='hp4.inp';
fn_out='hp4.out';
maxlen=100; { max length of the number }
type
hp=record
len:integer; { length of the number }
s:array[1..maxlen] of integer
{ s[1] is the lowest position
s[len] is the highest position }
end;
var
x:array[1..2] of hp;
y:hp; { x:input ; y:output }

procere PrintHP(const p:hp);
var i:integer;
begin
for i:=p.len downto 1 do write(p.s[i]);
end;

procere init;
var
st:string;
j,i:integer;
begin
assign(input,fn_inp);
reset(input);
for j:=1 to 2 do
begin
readln(st);
x[j].len:=length(st);
for i:=1 to x[j].len do { change string to HP }
x[j].s[i]:=ord(st[x[j].len+1-i])-ord('0');
end;
close(input);
end;

procere Multiply(a,b:hp;var c:hp); { c:=a+b }
var i,j,len:integer;
begin
fillchar(c,sizeof(c),0);
for i:=1 to a.len do
for j:=1 to b.len do
begin
inc(c.s[i+j-1],a.s[i]*b.s[j]);
inc(c.s[i+j],c.s[i+j-1] div 10);
c.s[i+j-1]:=c.s[i+j-1] mod 10;
end;
len:=a.len+b.len+1;
{
the proct of a number with i digits and a number with j digits
can only have at most i+j+1 digits
}
while(len>1)and(c.s[len]=0) do dec(len);
c.len:=len;
end;

procere main;
begin
Multiply(x[1],x[2],y);
end;

procere out;
begin
assign(output,fn_out);
rewrite(output);
PrintHP(y);
writeln;
close(output);
end;

begin
init;
main;
out;
end.

2.4 高精度除以高精度

程序如下:

program HighPrecision4_Multiply2;
const
fn_inp='hp6.inp';
fn_out='hp6.out';
maxlen=100; { max length of the number }
type
hp=record
len:integer; { length of the number }
s:array[1..maxlen] of integer
{ s[1] is the lowest position
s[len] is the highest position }
end;
var
x:array[1..2] of hp;
y,w:hp; { x:input ; y:output }

procere PrintHP(const p:hp);
var i:integer;
begin
for i:=p.len downto 1 do write(p.s[i]);
end;

procere init;
var
st:string;
j,i:integer;
begin
assign(input,fn_inp);
reset(input);
for j:=1 to 2 do
begin
readln(st);
x[j].len:=length(st);
for i:=1 to x[j].len do { change string to HP }
x[j].s[i]:=ord(st[x[j].len+1-i])-ord('0');
end;
close(input);
end;

procere Subtract(a,b:hp;var c:hp); { c:=a-b, suppose a>=b }
var i,len:integer;
begin
fillchar(c,sizeof(c),0);
if a.len>b.len then len:=a.len { get the bigger length of a,b }
else len:=b.len;
for i:=1 to len do { subtract from low to high }
begin
inc(c.s[i],a.s[i]-b.s[i]);
if c.s[i]<0 then
begin
inc(c.s[i],10);
dec(c.s[i+1]); { add 1 to a higher position }
end;
end;
while(len>1) and (c.s[len]=0) do dec(len);
c.len:=len;
end;

function Compare(const a,b:hp):integer;
{
1 if a>b
0 if a=b
-1 if a<b
}
var len:integer;
begin
if a.len>b.len then len:=a.len { get the bigger length of a,b }
else len:=b.len;
while(len>0) and (a.s[len]=b.s[len]) do dec(len);
{ find a position which have a different digit }
if len=0 then compare:=0 { no difference }
else compare:=a.s[len]-b.s[len];
end;

procere Multiply10(var a:hp); { a:=a*10 }
var i:Integer;
begin
for i:=a.len downto 1 do
a.s[i+1]:=a.s[i];
a.s[1]:=0;
inc(a.len);
while(a.len>1) and (a.s[a.len]=0) do dec(a.len);
end;

procere Divide(a,b:hp;var c,d:hp); { c:=a div b ; d:=a mod b }
var i,j,len:integer;
begin
fillchar(c,sizeof(c),0);
len:=a.len;
fillchar(d,sizeof(d),0);
d.len:=1;
for i:=len downto 1 do
begin
Multiply10(d);
d.s[1]:=a.s[i]; { d:=d*10+a.s[i] }
{ c.s[i]:=d div b ; d:=d mod b; }
{ while(d>=b) do begin d:=d-b;inc(c.s[i]) end }
while(compare(d,b)>=0) do
begin
Subtract(d,b,d);
inc(c.s[i]);
end;
end;
while(len>1)and(c.s[len]=0) do dec(len);
c.len:=len;
end;

procere main;
begin
Divide(x[1],x[2],y,w);
end;

procere out;
begin
assign(output,fn_out);
rewrite(output);
PrintHP(y);
writeln;
PrintHP(w);
writeln;
close(output);
end;
begin
init;
main;
out;
end.

㈥ 高精度乘法 pascal

var s:array[1..100] of integer;
k,i,j,n,l:integer;
o:boolean;
begin
readln(n); s[100]:=1;
for i:=1 to n-1 do begin
for j:=100 downto 1 do begin
s[j]:=s[j]*2+k;
if s[j]>9 then begin k:=s[j] div 10; s[j]:=s[j] mod 10; end else k:=0;
end;
end;
for i:=100 downto 1 do
if s[i]>9 then
begin
s[i-1]:=s[i-1]+s[i] div 10;
end;
o:=false;
for l:=1 to 100 do
if (s[l]<>0)or o then begin write(s[l] mod 10); o:=true; end;
writeln; readln;
end.

輸入輸出樣例:
輸入:3 {2的2次方}
輸出:4 {2的2次方的結果}

輸入:100 {2的99次方}
輸出: {2的99次方的結果}

㈦ 請幫我解析一下pascal高精度計算的乘法運算。謝謝

讀入被乘數s1,乘數s2
把s1、s2分成4位一段,轉成數值存在數組a,b中;記下a,b的長度k1,k2;
i賦為b中的最低位;
從b中取出第i位與a相乘,累加到另一數組c中;
i:=i-1;檢測i值:小於k2則轉⑥,否則轉④

㈧ 高精度乘法原理與演算法是什麼

模擬豎式乘法,逐位相乘,用字元形式儲存數字進行計算,結果仍為字元。演算法網上都有的。

閱讀全文

與高精度乘法演算法相關的資料

熱點內容
騰訊雲連接不上伺服器 瀏覽:221
不能用來表示演算法的是 瀏覽:859
6軸機器人演算法 瀏覽:890
手機主題照片在哪個文件夾 瀏覽:294
安卓手機後期用什麼軟體調色 瀏覽:628
cad修改快捷鍵的命令 瀏覽:242
好錢包app怎麼登錄不了 瀏覽:859
樹莓派都用python不用c 瀏覽:757
access文件夾樹的構造 瀏覽:662
安卓多指操作怎麼設置 瀏覽:658
linux樹形目錄 瀏覽:727
平方根的簡單演算法 瀏覽:898
千牛訂單頁面信息加密取消 瀏覽:558
單片機自製紅外遙控燈 瀏覽:719
伺服器最小配置怎麼弄 瀏覽:853
ibm伺服器硬體如何升級 瀏覽:923
全球程序員節點贊 瀏覽:986
php函數傳遞數組 瀏覽:632
人工峰群演算法的目標函數 瀏覽:468
如何刪加密文檔 瀏覽:105