69,368
社区成员
发帖
与我相关
我的任务
分享
int c(int x,int y) /*求杨辉三角形中第x行第y列的值*/
{
int z;
if((y==1) ¦ ¦(y==x+1)) return 1; /*若为x行的第1或第x+1列,则输出1*/
z=c(x-1,y-1)+c(x-1,y); /*否则,其值为前一行中第y-1列与第y列值之和*/
return z;
}
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#define N 10
#define SWAP(x, y) x^=y^=x^=y
int main()
{
int a[2][N];
int i, j;
int from = 0, curr = 1;
for (i = 0; i < N; printf("\n"), a[curr][(i++)+1] = 0, SWAP(curr, from))
for ( printf("%d\t",a[curr][0] = j = 1); j <= i; j++)
printf("%d\t", a[curr][j] = a[from][j] + a[from][j-1]);
system("pause");
return 0;
}