Experiment 2-3-6 Find the sum of the first N terms of the interleaved sequence (15 points)

This problem requires programming, calculates interlaced sequence 1-2/3+3/5-4/7+5/9-6/11+...and the first N entries.

Input format:

Enter a positive integer N in one line.

Output format:

The partial sum value is output in one line, and the result is kept to three decimal places.

Input sample:

5

Sample output:

0.917

Code:

# include <stdio.h>
# include <stdlib.h>

int main() {
    
    
    int N,i,j = 1;
    scanf("%d",&N);
    double sum = 0.0,m = 1.0;
    for (i=1;i<=N;i++) {
    
    
        sum += (i / m) * j;
        j *= (-1);
        m += 2;
    }
    printf("%.3lf",sum);
    return 0;
}

Submit screenshot:

Insert picture description here

Problem-solving ideas:

Look for regular problems. Everyone should look at him carefully. At the beginning, I regarded the denominator as a Fibonacci sequence, but it was always wrong. Later, I saw that the denominator is just a simple odd term! The rest of the operation is similar to the above question!

Guess you like

Origin blog.csdn.net/weixin_43862765/article/details/114390777