[PTA] 7-32 sequence before interleaving N seek and (15 minutes)

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

Input format:
input line in a given positive integer N.

Output format:
output section and a value in a row, the results of three decimal places.

Sample input:
5

Sample output:
0.917

#include <stdio.h>
int main()
{
    int n;
    double a=1,b=1,sum=0;
    int sign=1;
    scanf("%d",&n);

        for(int i=0;i<n;i++){
            sum+=a/b*sign;
            a++;
            b+=2;
            sign=-sign;
    }
        printf("%.3lf",sum);

    return 0;
}

Knowledge:
. 1, Sign = int. 1; for loop in sign = -sign, may represent a sign alternating
2,% 3lf three decimal places represents a double type output.

Published 48 original articles · won praise 0 · Views 310

Guess you like

Origin blog.csdn.net/weixin_46399138/article/details/105389876