#鹏哥C语言#P49
方法一:
#include<stdio.h>
int main()
{
int i=1;
double sum=0;
int flag=1;
for(i=1;i<=100;i++)
{
sum=sum+1.0/(i*flag);
flag=-flag;
}
printf("%f",sum);
return 0;
}
方法二:
#include<stdio.h>
int main()
{
int i=1;
double sum1=0,sum2=0,sum;
for(i=1;i<=100;i+=2)
{
sum1=sum1+1.0/i;
}
for(i=2;i<=100;i+=2)
{
sum2=sum2+1.0/i;
}
sum=sum1-sum2;
printf("%f",sum);
return 0;
}
运行结果展示: