题意:股票,给出前面到今天共k天的股票价格pi,从第i天开始算起前m天的平均股价为pm[i],从第i天开始算起前n天的平均股价为pn[i],m < n,一个人,若pm[i] > pn[i]且pm[i-1] < pn[i-1],说明今天股价涨了(或第n天pm[n] > pn[n]),他会买;若pm[i] < pn[i]且pm[i-1] > pn[i-1],说明今天股价跌了(或第n天pm[n] < pn[n]),他会卖,问从第n天开始,他在哪一天会买或卖股票,按先后顺序输出 (0 < pi < 100 for 1 <= i <= k,n < k <= 10 000, Pi(m) != Pi(n) for all i (n <= i <= k))。
题目链接:http://poj.org/problem?id=2163
——>>思路很直接,只是有个要注意的问题:pi是以2位小数输入的,为了精确,应乘上100变整处理。
#include <cstdio>using namespace std;const int maxn = 10000 + 10;
const double eps = 1e-8;int p[maxn], L[maxn], R[maxn];
long long sum[maxn];
double pm[maxn], pn[maxn];int main()
{int m, n, k;while(scanf("%d%d%d", &m, &n, &k) == 3){sum[0] = 0;for(int i = 1; i <= k; i++){scanf("%d.%d", &L[i], &R[i]);p[i] = L[i] * 100 + R[i];sum[i] = sum[i-1] + p[i];}for(int i = m; i <= k; i++) pm[i] = (double)(sum[i] - sum[i-m]) / m;for(int i = n; i <= k; i++) pn[i] = (double)(sum[i] - sum[i-n]) / n;if(pm[n] - pn[n] > eps) printf("BUY ON DAY %d\n", n);else if(pn[n] - pm[n] > eps) printf("SELL ON DAY %d\n", n);for(int i = n+1; i <= k; i++) if(pm[i] - pn[i] > eps && pn[i-1] - pm[i-1] > eps) printf("BUY ON DAY %d\n", i);else if(pn[i] - pm[i] > eps && pm[i-1] - pn[i-1] > eps) printf("SELL ON DAY %d\n", i);}return 0;
}