新手求助 麻烦各位看下 flag = -flag; 是怎么运算的
/*求1-1/2+1/3....的值
*/
# include <stdio.h>
int main(void)
{
int i, n, flag;
float sum = 0;
n = 3;
flag = 1 ;
for (i = 1; i <= n; ++i)
{
sum =sum +1.0/i*flag;
flag = -flag;
printf ("flag =%d\n", flag);
}
printf ("sum = %f\n", sum);
return 0;
}
/*
-----------------------------------------------------
flag =-1
flag =1
flag =-1
sum = 0.833333
我想问下这个“flag = 1”是怎么得出来的呢,
“flag = -flag”程序运行到这 不是总把 “flag = -1”
为什么会出现 flag =1
------------------------------------------------------
*/