C語言,計算1+1/2-1/3+1/4-1/5+1/6...,要求只使用一個循環
float foo(int n)
{
float acc = 1.0;
for (int i = 1; i < n; i++)
{
if (i % 2 == 0) acc += 1/float(i); else acc -= 1/float(i);
}
return acc;
}
int main()
{
float r = foo(100);
printf("%f", r);
}
http://codepad.org/tYlq33fQ