/*
求 1-1/2+1/3-...+1/99-1/100的和
*/
#include <stdio.h>
int main()
{
int s;
double i;
float sum;
s=1;
sum=1.0;
for(i=2;i<=100;i++)
{
s=-s;
sum=sum+s*(1/i);
}
printf("result is %f.\n",sum);
return 0;
}
输出结果:
result is 0.688172.