POJ2586Y2K Accounting Bug

http://poj.org/problem?id=2586

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
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<cstdlib>
 5 using namespace std ;
 6 int main()
 7 {
 8     int m,n;
 9     while(scanf("%d %d",&m,&n)!=EOF)
10     {
11         int flag = 0 ,surplus;
12         if(4*m < n)
13         {
14             surplus = 10*m-2*n ;
15             if(surplus < 0)
16             flag = 1;
17         }
18         else if(3*m < 2*n)
19         {
20             surplus = 8*m-4*n;
21             if(surplus < 0)
22             flag = 1;
23         }
24         else if(2*m < 3*n)
25         {
26             surplus=6*m-6*n;
27             if(surplus < 0)
28             flag = 1;
29         }
30         else if(m < 4*n)
31         {
32             surplus=3*m-9*n;
33             if(surplus < 0)//一开始加了分号在这里导致WA我竟然没发现。
34             flag = 1;
35         }
36         else
37          flag = 1;
38         if(flag == 1)
39         cout<<"Deficit"<<endl ;
40         else cout<<surplus<<endl ;
41     }
42     return 0 ;
43 }
View Code

这个时间的话是0MS;挺快的

看着分类在贪心里边,后来发现其实就五种情况,枚举一下也行

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<cstdlib>
 5 using namespace std ;
 6 int main()
 7 {
 8     int m,n;
 9     while(scanf("%d %d",&m,&n)!=EOF)
10     {
11         int flag = 0 ,surplus;
12         if(4*m < n)
13         {
14             surplus = 10*m-2*n ;
15         }
16         else if(3*m < 2*n)
17         {
18             surplus = 8*m-4*n;
19         }
20         else if(2*m < 3*n)
21         {
22             surplus=6*m-6*n;
23         }
24         else if(m < 4*n)
25         {
26             surplus=3*m-9*n;
27         }
28         else
29          surplus = -1;
30         if(surplus < 0)
31         cout<<"Deficit"<<endl ;
32         else cout<<surplus<<endl ;
33     }
34     return 0 ;
35 }
View Code

这个是AIHONGHONG帮我改的一开始我因为不仔细WA了,挺好的这个,时间是30MS

 1 #include<cstdio>
 2 #include<cstring>
 3 using namespace std;
 4 int main()
 5 {
 6     int n,m;
 7     while(scanf("%d%d",&n,&m)!=EOF){
 8        int k=0,i;
 9        if(n==0&&m==0)//不加这个就是16MS
10        {
11            printf("Deficit");
12            continue;
13        }
14        else if(n==0||m==0)
15        {
16            printf("Deficit");
17            continue;
18        }
19        for(i=1;i<=5;i++)
20        {
21            if(m*(5-i)-n*i>0)
22            {
23              k=i;
24            }
25            else break;
26        }
27        if(k==1)
28        {
29            if(3*k*n-((5-k)*2+1)*m<0)
30            printf("Deficit");
31            else printf("%d",3*k*n-((5-k)*2+1)*m);
32        }
33        else if(k>1)
34        {
35            if(2*k*n+2*n-((5-k)*2)*m<0)
36            printf("Deficit");
37            else printf("%d",2*k*n+2*n-((5-k)*2)*m);
38        }
39        else printf("Deficit");
40        printf("
");
41     }
42     return 0;
43 }
View Code

还有这个是会神代码

大神的博客http://www.cnblogs.com/justforgl/archive/2012/07/14/2591110.html

http://blog.csdn.net/lyy289065406/article/details/6642603

原文地址:https://www.cnblogs.com/luyingfeng/p/3239006.html