版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Fitz1318/article/details/86313314
题目链接:http://poj.org/problem?id=2586
大致题意:已知一个公司在某一年中,每个月要么固定盈利s、要么固定亏损d。 但是具体哪个月盈利、哪个月亏损却不得而知。不过可以肯定的是,这一年中,任意的连续5个月盈亏和必定是亏损。 问这年是否存在盈利的可能,若可能盈利,输出最大的盈利额,若不存在盈利,输出"Deficit"。
解题思路:
①假设所有的月均为盈利,判断第一个连续五月是亏损,如果不亏损,将最后一个月设为亏损,再次判断如果仍亏损的话让倒数第二个月设为亏损。。直到满足情况为止。
②依次判断8个连续的五月,最后检查总情况。
AC代码:
#include <iostream>
using namespace std;
int add(int m[], int n)
{
int i = 0;
int result = 0;
for(int i = n; i < n + 5; i++) {
result = result + m[i];
}
return result;
}
int main()
{
int d, s;
int month[12];
while(cin >> s >> d) {
for(int i = 1; i <= 12; i++) {
month[i] = s;
}
for(int i = 1; i <= 8; i++) {
for(int j = 1; j <= 5; j++) {
int flag = add(month, i);
if(flag > 0) {
month[i + 5 - j] = -d;
} else
break;
}
}
int surplus = 0;
for(int i = 1; i <= 12; i++) {
surplus += month[i];
}
if(surplus > 0) {
cout << surplus << endl;
} else
cout << "Deficit" << endl;
}
return 0;
}