我最近写了个杨辉三角的输出函数,但输出到7的时候就开始出现负数了.

来源:百度知道 编辑:UC知道 时间:2024/06/24 14:24:52
我的代码如下
#include "stdio.h"
int fac(int x)
{
int fas;
fas=1;
if(x==0)
fas=1;
else
fas=fac(x-1)*x;
return(fas);
}
void main()
{
int i,j,m,n;
printf("input number \n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
m=1;
printf("%d ",m);
for(j=1;j<=i;j++)
{
m=fac(i)/fac(j);
m=m/fac(i-j);
printf("%d ",m);
}
printf("\n");
}
getch();
}
输出结果:
Invalid keyboard code specified
input number
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 -5 -17 -35 -43 -35 -17 -5 1

我看了一下你的程序,你把fas=fac(x-1)*x; 和{m=fac(i)/fac(j);
m=m/fac(i-j);}这两组语句改改看,如果不行,就换一个软件试试,看能否可以得到想我的结果.

我运行了,没问题

# include <stdio.h>
int fac(int x)
{
int fas;
if(x==0)
fas=1;
else
fas=fac(x-1)*x;
return(fas);
}
void main()
{
int i,j,m,n;
printf("input number \n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
{
m=fac(i)/fac(j);
m=m/fac(i-j);
printf("%3d ",m);
}
printf("\n");
}
}
/*
input number
10
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
Press any key to continue
你可以这样,你那