pascal怎样用 递归做杨辉三角 (顺便帮我看看我的杨辉三角程序哪里有错)
programtriange;varn,i,j:integer;a:array[0..100,0..100]ofinteger;functionf(x,y:integer...
program triange;
var n,i,j:integer;
a:array[0..100,0..100]of integer;
function f(x,y:integer):integer;
begin
if x=1 then a[1,1]:=1
else begin
for i:=1 to n do
for j:=1 to n do
a[i,j]:=f(i-1,j-1)+f(i-1,j);
end;
end;
begin
fillchar(a,sizeof(a),0);
read(n);
f(n,1);
for i:=1 to n do begin
for j:=1 to n do if a[i,j]<>0 then write(a[i,j],' ');
writeln;
end;
end. 展开
var n,i,j:integer;
a:array[0..100,0..100]of integer;
function f(x,y:integer):integer;
begin
if x=1 then a[1,1]:=1
else begin
for i:=1 to n do
for j:=1 to n do
a[i,j]:=f(i-1,j-1)+f(i-1,j);
end;
end;
begin
fillchar(a,sizeof(a),0);
read(n);
f(n,1);
for i:=1 to n do begin
for j:=1 to n do if a[i,j]<>0 then write(a[i,j],' ');
writeln;
end;
end. 展开
2个回答
展开全部
function f(x,y:integer):integer;
begin
if x=1 then a[1,1]:=1
else begin
for i:=1 to n do
for j:=1 to n do
a[i,j]:=f(i-1,j-1)+f(i-1,j);
end;
end;
这里有问题
杨辉三角是每个地方等于左上、右上之和
参考程序
program triange;
var n,i,j:integer;
a:array[0..100,0..100]of longint;
begin
a[1,1]:=1;
for i:=2 to n do
for j:=1 to i do
if j=1 then a[i,j]:=1 else
if j=i then a[i,j]:=1 else
a[i,j]:=a[i-1,j-1]+a[i-1,j];
for i:=1 to n do begin
for j:=1 to n do write(a[i,j],' ');
writeln;
end;
begin
if x=1 then a[1,1]:=1
else begin
for i:=1 to n do
for j:=1 to n do
a[i,j]:=f(i-1,j-1)+f(i-1,j);
end;
end;
这里有问题
杨辉三角是每个地方等于左上、右上之和
参考程序
program triange;
var n,i,j:integer;
a:array[0..100,0..100]of longint;
begin
a[1,1]:=1;
for i:=2 to n do
for j:=1 to i do
if j=1 then a[i,j]:=1 else
if j=i then a[i,j]:=1 else
a[i,j]:=a[i-1,j-1]+a[i-1,j];
for i:=1 to n do begin
for j:=1 to n do write(a[i,j],' ');
writeln;
end;
追问
你这个不是递归。。。我想要递归的、。 但也非常感谢。。
追答
哦...递归...
这个用递归...好像不行啊
展开全部
参考程序
#include<stdio.h>
main()
{
int i,j,n=13;
printf("N=");
while(n>12)
scanf("%d",&n);
for(i=0;i<=n;i++)
{
for(j=0;j<24-2*i;j++) printf(" ");
for(j=1;j<i+2;j++) printf("%4d",(i,j));
printf("\n");
}
}
int c(int x,int y)
{
int z;
if((y==1)||(y==x+1)) return 1;
z=c(x-1,y-1)+c(x-1,y);
return z;
}
#include<stdio.h>
main()
{
int i,j,n=13;
printf("N=");
while(n>12)
scanf("%d",&n);
for(i=0;i<=n;i++)
{
for(j=0;j<24-2*i;j++) printf(" ");
for(j=1;j<i+2;j++) printf("%4d",(i,j));
printf("\n");
}
}
int c(int x,int y)
{
int z;
if((y==1)||(y==x+1)) return 1;
z=c(x-1,y-1)+c(x-1,y);
return z;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询