code4sk/c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第1次月考/求简单交错序列前N项和.cpp
2023-12-15 22:38:26 +08:00

35 lines
670 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
7-2 求简单交错序列前N项和
分数 10
作者 C课程组
单位 浙江大学
本题要求编写程序,计算序列 1 - 1/4 + 1/7 - 1/10 + ... 的前N项之和。
输入格式:
输入在一行中给出一个正整数N。
输出格式:
在一行中按照“sum = S”的格式输出部分和的值S精确到小数点后三位。题目保证计算结果不超过双精度范围。
输入样例:
10
输出样例:
sum = 0.819
*/
#include <stdio.h>
int main()
{
int N;
scanf("%d", &N);
int flag = 1;
int a = 1;
double sum = 0;
for (int i = 1; i <= N; i++, a += 3)
{
sum += 1.0 / a * flag;
flag = -flag;
}
printf("sum = %.3lf", sum);
return 0;
}