PASCAL语言怎么编杨辉三角
1个回答
2013-06-21
展开全部
program triangle;
function yh(i, j: longint): longint;
begin
if (j = 1) or (j = i) then
yh := 1
else
yh := yh(i - 1, j - 1) + yh(i - 1, j);
end;
var
n, i, j: integer;
begin
assign(input, 'triangle.in');
assign(output, 'triangle.out');
reset(input);
read(n);
close(input);
rewrite(output);
for i := 1 to n do
begin
write('1');
for j := 2 to i do
write(' ', yh(i, j));
writeln;
end;
close(output);
end.
function yh(i, j: longint): longint;
begin
if (j = 1) or (j = i) then
yh := 1
else
yh := yh(i - 1, j - 1) + yh(i - 1, j);
end;
var
n, i, j: integer;
begin
assign(input, 'triangle.in');
assign(output, 'triangle.out');
reset(input);
read(n);
close(input);
rewrite(output);
for i := 1 to n do
begin
write('1');
for j := 2 to i do
write(' ', yh(i, j));
writeln;
end;
close(output);
end.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询