33: calculates the value of the score addition and subtraction expression. 33 indicates the expression.

Source: Internet
Author: User

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 }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.