当前位置:网站首页>[C language] PTA 7-52 finding the sum of the first n terms of a simple interleaved sequence
[C language] PTA 7-52 finding the sum of the first n terms of a simple interleaved sequence
2022-07-29 04:29:00 【LastWhisperw】
This question requires the preparation of procedures , Calculation sequence 1 - 1/4 + 1/7 - 1/10 + ... Before N Sum of items .
Input format :
Input gives a positive integer on a line N.
Output format :
In a row, press “sum = S” The format of the output part and the value of S, Accurate to three decimal places . The calculation results should not exceed the double precision range .
sample input :
10No blank lines at the end
sample output :
sum = 0.819No blank lines at the end
#include<stdio.h>
int main(){
int N;
double sum=0;
double i;// The first i term
scanf("%d",&N);
int sign=1;// Symbol
for(i=1;i<=N;i++){
double a;
a=1/(3*i-2);
sum=sum+sign*a;
sign*=-1;
}
printf("sum = %.3f",sum);
return 0;
} 边栏推荐
- What is the difference between field, variable and property
- Pyqt5 learning pit encounter and pit drainage (1) unable to open designer.exe
- Interview notes of a company
- LeetCode_ Stack topics
- [k210 stepping pit] pytorch model is converted to kmodel and used on dock. (ultimately not achieved)
- [material delivery UAV] record (ROS + Px4 + yolov5 + esp8266 + steering gear)
- Laya中的A星寻路
- What is the use of meta-info?
- 不会就坚持62天吧 单词之和
- Mongo Shell交互式命令窗口
猜你喜欢

STL source code analysis (Hou Jie) notes - STL overview

LeetCode_ Stack topics

Star a pathfinding in LAYA

Use of construction methods

TypeError: Cannot read properties of undefined (reading ‘then‘)

It won't last for 65 days. It only appears once

Machine vision Series 2: vs DLL debugging

String, array, generalized table (detailed)

不会就坚持62天吧 单词之和

Won't you just stick to 62 days? Sum of words
随机推荐
Star a pathfinding in LAYA
TypeError: Cannot read properties of undefined (reading ‘then‘)
Use of torch.optim optimizer in pytorch
mpc5744p简介与OpenSDA固件更新
i++与++i详解
11.备份交换机
不会就坚持61天吧 最短的单词编码
Integration of Nan causes in pytorch training model
Jenkins 参数化构建中 各参数介绍与示例
JVM (heap and stack) memory allocation
Openfeign asynchronous call problem
settings.xml
用 ZEGO Avatar 做一个虚拟人|虚拟主播直播解决方案
redux快速上手
Labelme cannot open the picture
Machine vision series 3:vs2019 opencv environment configuration
pyscript无法引入包
Target detection learning process
Not for 63 days. The biggest XOR
STL source code analysis (Hou Jie) notes - STL overview