打印10行杨辉三角形,怎么输不出来?
#include <stdio.h>
void main()
{
int i,j;
int a[10][10];
printf( "\n ");
for(i=0,j=0;i <10;i++,j++) //每行第一个数最后一个是1;
{
a[i][0]=1; //执行时,这里报错;
a[i][j]=1;
}
for(i=2;i <10;i++)
{
for (j=1;j <i;j++)
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
for(i=0;i <10;i++)
{
for(j=0;j <=i;j++)
printf( "%d ",a[i][j]);
printf( "\n ");
}
}
[解决办法]
#include <stdio.h>
void main()
{
int i,j;
int a[10][10];
printf( "\n ");
for(i=0,j=0;i <10;i++,j++) //每行第一个数最后一个是1;
{
a[i][0]=1;
a[i][j]=1;
}
for(i=2;i <10;i++)
{
for (j=1;j <i;j++)
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
for(i=0;i <10;i++)
{
for(j=0;j <=i;j++)
printf( "%d ",a[i][j]);
printf( "\n ");
}
return;
}
你切换了输入法吧
去掉好像就没错了
[解决办法]
a[i][0]=1; //执行时,这里报错;
a[i][j]=1; //执行的时候J超出了范围 〉10了
[解决办法]
#include <stdio.h>
#include <string.h>
int main()
{
int n,a[32],b[32],i,j;
while(scanf( "%d ",&n)!=EOF)
{
a[1] = a[2] = b[1] = b[2] = 1;
printf( "1\n ");
for(i = 2; i <= n; i++)
{
printf( "1 ");
for(j = 2; j < i; j++)
{
b[j] = a[j-1]+a[j];
printf( "%d ",b[j]);
}
b[j] = 1;
memcpy(a,b,sizeof(int)*(n+2));
printf( "1\n ");
}
printf( "\n ");
}
return 0;
}
[解决办法]
[root@shwhg test]# gcc test.c
test.c: In function `main ':
test.c:4: warning: return type of 'main ' is not `int '
[root@shwhg test]# ./a.out
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
没错呀,
[解决办法]
程序真确
printf ( "%-5d ", a[i][j]);//改这个地方,看着就清楚了
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