计算2支股票的M天运动平均价格

题解
  1. 注意这里的年和月并不是固定为2004/7的!年月日都会变!!!(悲
  2. 二维数组a1和a2分别存类型为open和close的数据,并且这里将每组数据中的年月日信息和s1、s2价格全都存进一个一维数组。
题目
问题 M: 计算2支股票的M天运动平均价格
时间限制: 1 Sec  内存限制: 128 MB
提交: 439  解决: 213
[提交][状态][讨论版]
题目描述
给定2支股票的开盘价和收盘价的N天历史数据,
要求按开盘和收盘,分别计算每支股票的每个日期对应的M天移动平均价格。
假定两个股票数据如下:     
日期            开盘/收盘      第1支股票价格S1       第2支股票价格S2
2004/7/29  	close	64
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
按M=2天计算移动平均价格,按先开盘,后收盘价,输出如下:(若某日期之前,没有M-1条的记录(日期不用连续),则不用输出)
2004/7/28  	open	6	7
2004/7/30  	open	3	3
2004/7/27  	close	5	8
2004/7/29  	close	7	7
其中, 2004/7/28日的S1的值为(8+4)/2 = 6, 即将2004/7/28和(最近1条记录2004/7/26,最近2条记录,最近M-1条记录)的价格,求和并计算平均。
输入
第1行:N天记录   M天平均
第2行到N+1行:N天2支股票的开盘与收盘价格(注意日期是无序的)
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
输出
每个日期的最近M条记录(包括该日期的价格在内)的平均价格(若某日期之前没有M-1条的记录(日期不用连续),则不用输出)
2004/7/28  	open	6	7
2004/7/30  	open	3	3
2004/7/27  	close	5	8
2004/7/29  	close	7	7
样例输入
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
样例输出
2004/7/28 open 6 7
2004/7/30 open 3 3
2004/7/27 close 5 8
2004/7/29 close 7 7
代码块
#include <iostream>
#include <stdio.h>
using namespace std;

int main(void)
{
    int i, j, n, m;
    cin>>n>>m;
    int a1[n][5];
    int a2[n][5];
    int num1 = 0;
    int num2 = 0;
    for(i=0; i<n; i++)
    {
        int year, month, day, temp1, temp2;
        string temp;
        scanf("%d/%d/%d", &year, &month, &day);
        cin>>temp>>temp1>>temp2;
        if(temp=="open")
        {
            a1[num1][0] = year;
            a1[num1][1] = month;
            a1[num1][2] = day;
            a1[num1][3] = temp1;
            a1[num1][4] = temp2;
            num1++;
        }
        else if(temp=="close")
        {
            a2[num2][0] = year;
            a2[num2][1] = month;
            a2[num2][2] = day;
            a2[num2][3] = temp1;
            a2[num2][4] = temp2;
            num2++;
        }
    }
    int temp[5];
    for(i=0; i<num1-1; i++)
    {
        for(j=0; j<num1-i-1; j++)
        {
            if((a1[j][0]>a1[j+1][0]) || (a1[j][0]==a1[j+1][0] && a1[j][1]>a1[j][1]) || (a1[j][0]==a1[j+1][0] && a1[j][1]==a1[j+1][1] && a1[j][2]>a1[j+1][2]))
            {
                temp[0] = a1[j][0];
                temp[1] = a1[j][1];
                temp[2] = a1[j][2];
                temp[3] = a1[j][3];
                temp[4] = a1[j][4];
                a1[j][0] = a1[j+1][0];
                a1[j][1] = a1[j+1][1];
                a1[j][2] = a1[j+1][2];
                a1[j][3] = a1[j+1][3];
                a1[j][4] = a1[j+1][4];
                a1[j+1][0] = temp[0];
                a1[j+1][1] = temp[1];
                a1[j+1][2] = temp[2];
                a1[j+1][3] = temp[3];
                a1[j+1][4] = temp[4];
            }
        }
    }
    for(i=m-1; i<num1; i++)
    {
        int temp1 = 0, temp2 = 0;
        for(j=i-m+1; j<=i; j++)
        {
            temp1 += a1[j][3];
            temp2 += a1[j][4];
        }
        cout<<a1[i][0]<<'/'<<a1[i][1]<<'/'<<a1[i][2]<<' '<<"open"<<' '<<temp1/m<<' '<<temp2/m<<endl;
    }
    for(i=0; i<num2-1; i++)
    {
        for(j=0; j<num2-i-1; j++)
        {
            if((a2[j][0]>a2[j+1][0]) || (a2[j][0]==a2[j+1][0] && a2[j][1]>a2[j][1]) || (a2[j][0]==a2[j+1][0] && a2[j][1]==a2[j+1][1] && a2[j][2]>a2[j+1][2]))
            {
                temp[0] = a2[j][0];
                temp[1] = a2[j][1];
                temp[2] = a2[j][2];
                temp[3] = a2[j][3];
                temp[4] = a2[j][4];
                a2[j][0] = a2[j+1][0];
                a2[j][1] = a2[j+1][1];
                a2[j][2] = a2[j+1][2];
                a2[j][3] = a2[j+1][3];
                a2[j][4] = a2[j+1][4];
                a2[j+1][0] = temp[0];
                a2[j+1][1] = temp[1];
                a2[j+1][2] = temp[2];
                a2[j+1][3] = temp[3];
                a2[j+1][4] = temp[4];
            }
        }
    }
    for(i=m-1; i<num2; i++)
    {
        int temp1 = 0, temp2 = 0;
        for(j=i-m+1; j<=i; j++)
        {
            temp1 += a2[j][3];
            temp2 += a2[j][4];
        }
       cout<<a2[i][0]<<'/'<<a2[i][1]<<'/'<<a2[i][2]<<' '<<"close"<<' '<<temp1/m<<' '<<temp2/m<<endl;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值