Description questions
Programming, input the value of n, seeking 1/1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + 1/7 - 1/8 + ... + (-1) n value -1 · 1 / n of.
Input Format
Enter a positive integer n. 1 <= n <= 1000.
Output Format
Output a real number, the value of expression retained to four decimal places.
Sample input
2
Sample Output
0.5000
#include <stdio.h>
int main()
{
int n,i;
float a,sum;
while(scanf("%d",&n)!=EOF)
{
sum=0; a=1;
for(i=0;i<n;i++)
{
sum+=1/a;
if(a>0)
{
a=-(a+1);
continue; //此处如果没有continue,则会进入下一个if,达不到程序预期
}
if(a<0)
{
a=-(a-1);
continue;
}
}
printf("%.4f\n",sum);
}
return 0;
}