问题:
杨辉三角
#include <stdio.h>
#include <stdlib.h>
int c(int x, int y);
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int i, j, n;
printf("请输入杨辉三角的行数:");
scanf("%d", &n);
for(i=1; i<=n; i++)
{
for(j=0; j<=n-i; j++)
printf(" ");
for(j=1; j<=i; j++)
printf("%4d", c(i, j));
printf("\n");
}
return 0;
}
int c(int x, int y)
{
int z;
if(y==1 || y==x)
return 1;
else
{
z = c(x-1, y-1)+c(x-1,y);//对于x行的第y个值等于x-1行的第y-1个值和第y个值得和
return z;
}
}
/*
请输入杨辉三角的行数: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
--------------------------------
*/
原文地址:http://blog.csdn.net/orangeisnotapple/article/details/44871713