我试图在我的程序中找到一个解决方案(修复错误),它必须从定义中计算二项式定理。首先,我创建了“阶乘”-“silnia”的定义。
1) 算法确定定义的SN1(n,k)的值。(牛顿函数)
2) 算法通过公式递归确定SN3(n,k)的值。(newton_rek函数)。
输入: 文件名:In0101.txt
OUTPUT: 文件名:Out0101.txt 在这个文件中我想保存从公式计算出来的值。
示例: In0101.txt
8 2// n k
Out0101.txt
n=8 k=2
SN1 = 28; count= 14
还有一个我无法修复的错误。有人可以帮我吗?
我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
long silnia(int a)
{
long s;
if (a == 0 || a == 1)
{
return 1;
}
else
{
s = 1;
for (int i = 1; i <= a; i++)
{
s *= i;
}
}
return s;
}
long newton(int n, int k)
{
return silnia(n)/(silnia(k)*silnia(n-k));
}
unsigned long int newton_rek(long int n ,long int k)
{
if ( n == k || k == 0 )
{
return 1;
}
if (k > n)
{
return 0;
}
else return newton_rek(n-1,k-1) + newton_rek(n-1,k);
}
int main()
{
int n = 0;
int k = 0;
long funkcja1 = 0;
long funkcja2 = 0;
FILE *f = fopen("In0101.txt", "r+");
if (f == NULL)
{
printf("Nie udalo sie otworzyc pliku In0101.txt\n");
return 1;
}
fread(n, sizeof(long), 1 , f);
fread(k, sizeof(long), 1 , f);
fclose(f);
FILE *ff = fopen("Out0101.txt", "w+");
if (ff == NULL)
{
printf("Nie udalo sie otworzyc pliku Out0101.txt\n");
return 1;
}
funkcja1 = newton(n,k);
funkcja2 = newton_rek(n,k);
fwrite(funkcja1, sizeof(long), 1 , ff);
fwrite(funkcja2, sizeof(long), 1 , ff);
fclose(f);
return 0;
}