POJ2586——Y2K Accounting Bug

Y2K Accounting Bug
 

Description

Accounting for Computer Machinists (ACM) has sufferred from the Y2K bug and lost some vital data for preparing annual report for MS Inc. 
All what they remember is that MS Inc. posted a surplus or a deficit each month of 1999 and each month when MS Inc. posted surplus, the amount of surplus was s and each month when MS Inc. posted deficit, the deficit was d. They do not remember which or how many months posted surplus or deficit. MS Inc., unlike other companies, posts their earnings for each consecutive 5 months during a year. ACM knows that each of these 8 postings reported a deficit but they do not know how much. The chief accountant is almost sure that MS Inc. was about to post surplus for the entire year of 1999. Almost but not quite. 

Write a program, which decides whether MS Inc. suffered a deficit during 1999, or if a surplus for 1999 was possible, what is the maximum amount of surplus that they can post.

Input

Input is a sequence of lines, each containing two positive integers s and d.

Output

For each line of input, output one line containing either a single integer giving the amount of surplus for the entire year, or output Deficit if it is impossible.

Sample Input

59 237
375 743
200000 849694
2500000 8000000

Sample Output

116
28
300612
Deficit

题目大意:微软公司的12个月的盈利情况,已知每月若盈利,则固定为s元;若亏损,则固定为d元。在保证每个连续的五个月都是亏损的情况下,判断是否可以全年盈利,若盈利,输出最大盈利额,反之,输出Deficit
解题思路:贪心的思想,保证每个连续5个月亏损最少即可。
     eg: s=59 d=237 那么连续五个月中 4个月盈利,一个月亏损时连续亏损最小,为-1;
     假设[1,5]表示一月到五月的总亏损,保证[1,5]=-1,那么[2,6]要想同样最小 六月的盈亏情况应与一月相同。
     同理:七月与二月相同,八月与三月相同。显然六到十月份的盈亏状态应同一月到五月的。
     显然:十一月与十二月 分别对应 一月和二月。
这里设盈利为1,亏损为0 [1-5]=11110||10111||11011.... 要想最大盈利,要保证最后两个月尽量盈利,及保证一月二月最大盈利,故前五个月应保证盈利月在前
Code:
 1 #include<stdio.h>
 2 int main()
 3 {
 4     int s,d,sum,cnt;
 5     while (scanf("%d %d",&s,&d)!=EOF)
 6     {
 7         sum=s*5;
 8         cnt=0;
 9         while (sum>=0)
10         {
11             sum-=d+s;
12             cnt++;
13         }
14         sum=(s*(5-cnt)-cnt*d)*2;//前十个月的总盈亏
15         if (cnt==4) {sum+=s;sum-=d;}//判断最后两月的盈亏状况
16         else if (cnt==5) sum-=2*d;
17         else sum+=2*s;
18         if (sum>0) printf("%d
",sum);//sum<0 说明亏损
19         else printf("Deficit
");
20     }
21     return 0;
22 }
原文地址:https://www.cnblogs.com/Enumz/p/3763172.html