我试图用C帕斯卡三角的解决方案,下面公式基于:帕斯卡三角形溶液
#include <stdio.h>
#include <stdlib.h>
int pascalTriangle(int row, int col);
int main()
{
int row, col;
printf("Enter the row [0 to n]: ");
scanf("%i", &row);
printf("Enter the column [0 to m]: ");
scanf("%i", &col);
if(col > row) {
printf("Error: column can be less than or equal to row\n");
exit(1);
}
printf("Value = %i\n", pascalTriangle(row, col));
return 0;
}
int pascalTriangle(int row, int col)
{
int value[100];
value[0]=1;
int i=1;
if(row==0 || row==col || col==0) {
return value[0];
} else {
row=row+1;
while(i<=col) {
printf("i = %i\trow = %i\tcol = %i\n", i, row, col);
value[i]='\0';
value[i]=(value[i-1]) * ((row-i)/i);
printf("value[%i] = %i\tvalue[%i] = %i\n", i-1, value[i-1], i, value[i]);
++i;
}
return value[i-1];
}
}
:
我基于上述式写下面的代码
在这里,它在一定程度上是给予适当的O/P。但是,对于很多I/P我发现了错误的答案。我无法找到逻辑错误,因为在纸面上逻辑给出了预期的O/P。例如: - 我给行= 4 & col = 2,O/P应该是6,但得到4作为O/P。
请帮忙!!
此外,您编写的公式是递归的,但您使用迭代来解决问题。你确定你正确地重写了递归吗? – 2013-02-15 15:53:42