POJ2586--Y2K Accounting Bug(贪心)

来源:互联网 发布:好的漫画软件 编辑:程序博客网 时间:2024/05/16 14:47

Do more with less

Discussion

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

题意

微软公司由于某些原因财务数据丢失,但知道每连续的5个月是亏损的(比如1.2.3.4.5月,2.3.4.5.6),且知道每个月的盈亏是个定值,如果这一年可以盈利则输出最大的盈利额,否则输出Deficit。

思路

贪心,在保证每连续五个月是亏损的前提下,尽量让亏损月最少就可以。

代码

#include <iostream>using namespace std;int main(){    int s,d;    while(cin>>s>>d)    {        int i = 4;        while(i > 0)        {            if(s*i < d*(5-i))                break;            i--;        }        switch(i)        {        case 0 :            {                cout<<"Deficit"<<endl;                break;            }        case 4 :            {                int ans = s*10 - d*2;                if(ans > 0)                    cout<<ans<<endl;                else                    cout<<"Deficit"<<endl;                break;            }        case 3 :           {                int ans = s*8 - d*4;                if(ans > 0)                    cout<<ans<<endl;                else                    cout<<"Deficit"<<endl;                break;           }        case 2 :           {                int ans = s*6 - d*6;                if(ans > 0)                    cout<<ans<<endl;                else                    cout<<"Deficit"<<endl;                break;           }        case 1 :            {                int ans = s*3 - d*9;                if(ans > 0)                    cout<<ans<<endl;                else                    cout<<"Deficit"<<endl;                break;           }        }    }}