/*
考虑这两个无限序列:
1.0 + 1.0/2.0 + 1.0/3.0 + 1.0/4.0 + ...
1.0 - 1.0/2.0 + 1.0/3.0 - 1.0/4.0 + ...
编写一个程序来计算这两个序列不断变化的总和,直到达到某个次数。让用户交互地输入这个次数。
*/
#include <stdio.h>
int main (void)
{
int j, fcf, i;
double sum1, sum2;
printf ("请输入次数:");
while (1 == scanf ("%d",&i))
{
for (sum1 = 0, j = 1, sum2 = 0, fcf = -1; j <= i; ++j)
{
fcf *= (-1);
sum1 = sum1 + (1.0 / i);
sum2 = sum2 + (1.0 / i) * fcf;
}
printf ("%lf %lf\n",sum1,sum2);
printf ("请输入另一个次数:");
}
system ("pause");
return 0;
}
Rober_Lin
相关分类