33: calculates the value of the score addition and subtraction expression. 33 indicates the expression.
33: calculate the value of the score addition and subtraction expression
- View
- Submit
- Statistics
- Question
-
Total time limit:
-
1000 ms
-
Memory limit:
-
65536kB
-
Description
-
Write a program and enter the value of n to calculate 1/1-1/2 + 1/3-1/4 + 1/5-1/6 + 1/7-1/8 +... + (-1) n-1 · 1/n value.
-
Input
-
Enter a positive integer n. 1 <= n <= 1000.
-
Output
-
Output a real number, which is the value of the expression and is retained to the fourth digit after the decimal point.
-
Sample Input
-
2
-
Sample output
-
0.5000
1 e<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 int main() 7 { 8 int n; 9 cin>>n;10 double tot=0;11 double fm=1; 12 for(int i=1;i<=n;i++)13 {14 if(i%2==0)15 {16 tot=tot-(1/fm);17 fm++;18 } 19 else 20 {21 tot=tot+(1/fm);22 fm++;23 }24 }25 printf("%.4lf",tot);26 return 0;27 }