主题:求大家帮我加个注释。。
对于阶乘函数,即使自变量较小,其函数值也会相当大。例如:
10!=32628800
25!=15511210043330985984000000
设计一个程序,当键入一个正整数N(1≤N≤100)时,输出N!的精确表示法。
本人先把Matodied解题的程序列出来:
TYPE arr = ARRAY[1..1023] OF INTEGER;
FUNCTION gjdmult(x, y: STRING): STRING;
VAR
a, b, s: arr;
l, lx, ly, d, i, j, k, v: INTEGER; z, zz: STRING;
BEGIN
lx := LENGTH(x); ly := LENGTH(y);
FOR i:=1 TO lx + ly DO s[i] := 0;
FOR i:=1 TO lx DO VAL(COPY(x, lx + 1 - i, 1), a[i], k);
FOR i:=1 TO ly DO VAL(COPY(y, ly + 1 - i, 1), b[i], k);
FOR i:=1 TO lx DO BEGIN
FOR j:=1 TO ly DO BEGIN
d := a[i] * b[j]; v := i + j - 1;
s[v] := s[v] + d MOD 10;
s[v + 1] := s[v + 1] + s[v] DIV 10 + d DIV 10;
s[v] := s[v] MOD 10;
END;
END;
IF s[lx + ly] = 0 THEN l := lx + ly - 1 ELSE l := lx + ly;
z := '';
FOR i:=l DOWNTO 1 DO BEGIN
STR(s[i], zz);
z := z + zz;
END;
gjdmult := z;
END;
VAR
a, b, l: STRING; n: WORD; i: INTEGER;
BEGIN
READLN(n); l := '1';
FOR i:=2 TO n DO BEGIN
a := l; STR(i, b);
l := gjdmult(a, b);
END;
WRITELN(n, '!=', l);
END.
前2天。Matodied就给了我这么一个程序。可我有一点看不懂。希望大家给我加个注释
并不是全加。加一部分就可以了~就是这一部分:
FOR i:=1 TO lx DO BEGIN
FOR j:=1 TO ly DO BEGIN
d := a[i] * b[j]; v := i + j - 1;
s[v] := s[v] + d MOD 10;
s[v + 1] := s[v + 1] + s[v] DIV 10 + d DIV 10;
s[v] := s[v] MOD 10;
END;
END;
尤其是这一步:s[v] := s[v] MOD 10;
越详细越好~。谢了哦~
10!=32628800
25!=15511210043330985984000000
设计一个程序,当键入一个正整数N(1≤N≤100)时,输出N!的精确表示法。
本人先把Matodied解题的程序列出来:
TYPE arr = ARRAY[1..1023] OF INTEGER;
FUNCTION gjdmult(x, y: STRING): STRING;
VAR
a, b, s: arr;
l, lx, ly, d, i, j, k, v: INTEGER; z, zz: STRING;
BEGIN
lx := LENGTH(x); ly := LENGTH(y);
FOR i:=1 TO lx + ly DO s[i] := 0;
FOR i:=1 TO lx DO VAL(COPY(x, lx + 1 - i, 1), a[i], k);
FOR i:=1 TO ly DO VAL(COPY(y, ly + 1 - i, 1), b[i], k);
FOR i:=1 TO lx DO BEGIN
FOR j:=1 TO ly DO BEGIN
d := a[i] * b[j]; v := i + j - 1;
s[v] := s[v] + d MOD 10;
s[v + 1] := s[v + 1] + s[v] DIV 10 + d DIV 10;
s[v] := s[v] MOD 10;
END;
END;
IF s[lx + ly] = 0 THEN l := lx + ly - 1 ELSE l := lx + ly;
z := '';
FOR i:=l DOWNTO 1 DO BEGIN
STR(s[i], zz);
z := z + zz;
END;
gjdmult := z;
END;
VAR
a, b, l: STRING; n: WORD; i: INTEGER;
BEGIN
READLN(n); l := '1';
FOR i:=2 TO n DO BEGIN
a := l; STR(i, b);
l := gjdmult(a, b);
END;
WRITELN(n, '!=', l);
END.
前2天。Matodied就给了我这么一个程序。可我有一点看不懂。希望大家给我加个注释
并不是全加。加一部分就可以了~就是这一部分:
FOR i:=1 TO lx DO BEGIN
FOR j:=1 TO ly DO BEGIN
d := a[i] * b[j]; v := i + j - 1;
s[v] := s[v] + d MOD 10;
s[v + 1] := s[v + 1] + s[v] DIV 10 + d DIV 10;
s[v] := s[v] MOD 10;
END;
END;
尤其是这一步:s[v] := s[v] MOD 10;
越详细越好~。谢了哦~