#include<stdio.h>
#include<math.h>
double factorial(int n);
int main()
{
float c =exp(0.5);
float a, b, s1, s2;
int n;
a = pow(0.5, n) / factorial(n);
b = pow(0.5, n+1) / factorial(n+1);
for(n =1; fabs(s1 = (b - a) / b) > 0.0005; n++)
{
s2 = (c - a) / c;
printf("n = %d, s1 = %f, s2 = %f.n", n, s1, s2);
}
}
double factorial(int n)
{
double temp =1;
int i;
for(i = 1; i <= n; i++)
{
temp *= i;
}
return temp;
}
排版排排好!!!!
可以这样来确定n
PS: 我试了下,貌似找不到这个n的值。 你参考下
unsigned long fact(unsigned int n)
{
if(n <= 1)
return 1;
else
return n*fact(n-1);
}
double A(int n)
{
return pow(0.5, n)/fact(n);
}
int main(void)
{
double a,b,s1,s2;
int n;
for(n=1; ; n++)
{
a = A(n);
b = A(n+1);
s1 = (b-a)/b;
if(fabs(s1) < 0.0005)
{
printf("n=%dn", n);
break;
}
}
return 0;
}