当前位置:网站首页>Calculate the M-day moving average price of two stocks
Calculate the M-day moving average price of two stocks
2022-07-24 14:49:00 【Running star dailu】
Title Description
Given 2 The opening price and closing price of a stock N Days of historical data ,
It is required to press the opening and closing , Calculate the corresponding M Daily moving average price .
Suppose two stock data are as follows :
date The opening quotation / The close The first 1 Stock price S1 The first 2 Stock price S2
2004/7/29 close 6 4
2004/7/25 close 2 6
2004/7/26 open 8 12
2004/7/30 open 2 4
2004/7/27 close 8 10
2004/7/28 open 4 2
Press M=2 Calculate the moving average price in days , Open first , Post closing price , Output is as follows :( If before a certain date , No, M-1 A record of ( The dates need not be consecutive ), No output is required )
2004/7/28 open 6 7
2004/7/30 open 3 3
2004/7/27 close 5 8
2004/7/29 close 7 7
among , 2004/7/28 Japanese S1 The value of is (8+4)/2 = 6, the 2004/7/28 and ( lately 1 Bar record 2004/7/26, lately 2 Bar record , lately M-1 Bar record ) The price of , Sum and average .
Input
The first 1 That's ok :N Day record M The average day
The first 2 Row to N+1 That's ok :N God 2 Opening and closing prices of stocks ( Notice that dates are out of order )
6 2
2004/7/29 close 6 4
2004/7/25 close 2 6
2004/7/26 open 8 12
2004/7/30 open 2 4
2004/7/27 close 8 10
2004/7/28 open 4 2
Output
Latest of each date M Bar record ( Including the price on that date ) Average price of ( If there is no M-1 A record of ( The dates need not be consecutive ), No output is required )
2004/7/28 open 6 7
2004/7/30 open 3 3
2004/7/27 close 5 8
2004/7/29 close 7 7
The sample input
6 2
2004/7/29 close 6 4
2004/7/25 close 2 6
2004/7/26 open 8 12
2004/7/30 open 2 4
2004/7/27 close 8 10
2004/7/28 open 4 2Sample output
2004/7/28 open 6 7
2004/7/30 open 3 3
2004/7/27 close 5 8
2004/7/29 close 7 7Code
#include "iostream"
#include "cstdio"
#include "algorithm"
#include "string"
using namespace std;
struct stock{
int y,m,d,s1,s2;
};
bool cmp(stock a,stock b){
if(a.y==b.y){
if(a.m==b.m){
return a.d<b.d;
}
return a.m<b.m;
}
return a.y<b.y;
}
class stocklist{
private:
stock *openlist;
stock *closelist;
int sum1,sum2;
public:
stocklist(){
sum1=sum2=0;
int N,M;
cin>>N>>M;
openlist=new stock[N+1];
closelist=new stock[N+1];
for(int i=1;i<=N;i++){
int y, m, d, s1, s2;
string op;
scanf("%d/%d/%d", &y, &m, &d);
cin >> op;
cin >> s1 >> s2;
// cout << y << " " << m << " " << d << " " << op << " " << s1 << " " << s2 << endl;
if(op=="open"){
sum1++;
openlist[sum1]=(stock){y,m,d,s1,s2};
}
else{
sum2++;
closelist[sum2]=(stock){y,m,d,s1,s2};
}
}
sort(openlist+1,openlist+sum1+1,cmp);
sort(closelist+1,closelist+sum2+1,cmp);
for(int i=M;i<=sum1;i++){
int ans1=0,ans2=0;
for(int j=i,k=0;k<M;k++){
ans1+=openlist[j-k].s1;
ans2+=openlist[j-k].s2;
}
ans1/=M;
ans2/=M;
printf("%d/%d/%d open %d %d\n",openlist[i].y,openlist[i].m,openlist[i].d,ans1,ans2);
}
for(int i=M;i<=sum2;i++){
int ans1=0,ans2=0;
for(int j=i,k=0;k<M;k++){
ans1+=closelist[j-k].s1;
ans2+=closelist[j-k].s2;
}
ans1/=M;
ans2/=M;
printf("%d/%d/%d close %d %d\n",closelist[i].y,closelist[i].m,closelist[i].d,ans1,ans2);
}
}
~stocklist(){
delete []openlist;
delete []closelist;
}
};
int main(){
// freopen("123.in","r",stdin);
stocklist list;
return 0;
}边栏推荐
- 自动化渗透扫描工具
- Overall testing framework for performance testing
- 小熊派 课程导读
- Bibliometrix: dig out the one worth reading from thousands of papers!
- Error when using Fiddler hook: 502 Fiddler - connection failed
- 正则表达和绕过案例
- ISPRS2018/云检测:Cloud/shadow detection based on spectral indices for multi/hyp基于光谱指数的多/高光谱光学遥感成像仪云/影检测
- Comparison of traversal speed between map and list
- 股票开户之后就可以购买6%的理财产品了?
- “00后”来了!数睿数据迎来新生代「无代码」生力军
猜你喜欢

Kotlin类与继承

深度学习中的学习率调整策略(1)

Sword finger offer II 001. integer division

Overview of dobesie wavelet (DB wavelet function) in wavelet transform

Video game design report template and resources over the years

ISPRS2018/云检测:Cloud/shadow detection based on spectral indices for multi/hyp基于光谱指数的多/高光谱光学遥感成像仪云/影检测

Learning and thinking about the relevant knowledge in the direction of building network security knowledge base

Bibliometrix: dig out the one worth reading from thousands of papers!

Notes on the use of IEEE transaction journal template

Spark Learning Notes (III) -- basic knowledge of spark core
随机推荐
[oauth2] III. interpretation of oauth2 configuration
Problem handling of repeated restart during Siemens botu installation
Unity 使用NVIDIA FleX for Unity插件实现制作软体、水流流体、布料等效果学习教程
Not configured in app.json (uni releases wechat applet)
mysql
Deep learning 1 perceptron and implementation of simple back propagation network
Strongly connected component
Under multi data source configuration, solve org.apache.ibatis.binding Bindingexception: invalid bound statement (not found) problem
JS judge whether it is an integer
Solve the problem that uni starter can log in to wechat with local functions, but fails to log in with cloud functions
Google Earth Engine——使用MODIS数据进行逐月数据的过火(火灾)面积并导出
How to set packet capturing mobile terminal
Data analysis and mining 2
Activate the newly installed Anaconda in the server
2022 IAA industry category development insight series report - phase II
Problems needing attention in mobile terminal testing
Preparation of mobile end test cases
Kotlin class and inheritance
Video game design report template and resources over the years
Leetcode · daily question · 1184. distance between bus stops · simulation