#include
int main()
{
int i, numer = 1, denom = 2, t; //numer为分子,denom为分母
double sum;
for(i = 0, sum = 0; i < 20; i++)
{
sum += (double)numer/denom;
t = numer; //保存分子的值
numer = -denom; //后一项分子为前一项分母的相反数
denom += t; //后一项分母为前一项分子分母之和
}
printf("%f",sum);
return 0;
}
#include
int main()
{
int i, numerator = 1, denominator = 2, t; //numerator为分子,denominator为分母
double sum;
for(i = 0, sum = 0; i < 20; i++)
{
sum = sum + (double)numerator/denominator;
//更高效的方法是 sum += (double)numerator/denominator;
t = numerator;
numerator = -denominator ; //后一项分子为前一项分母的相反数
denominator = denominator + t; //后一项分母为前一项分子分母之和
//更高效的方法是 denominator += t;
}
printf("%f",sum);
return 0;
}
#include
int main()
{
int i,j,k;
double sign = 1;
double sum = 0;
i = 1;
j = 2;
for(k=0;k<20;k++)
{
sum += i*sign/j;
sign *= -1;
j = i+j;
i = j-i;
}
printf("%lf\n",sum);
}
int main()
{
int i,j,k;
double sign = 1;
double sum = 0;
i = 1;
j = 2;
for(k=0;k<20;k++)
{
sum += i*sign/j;
sign *= -1;
j = i+j;
i = j-i;
}
printf("%lf\n",sum);
}