poj 2586 Y2K Accounting Bug贪心刷题计划
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj 2586 Y2K Accounting Bug贪心刷题计划相关的知识,希望对你有一定的参考价值。
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 16154 | Accepted: 8111 |
Description
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
Output
Sample Input
59 237 375 743 200000 849694 2500000 8000000
Sample Output
116 28 300612 Deficit
Source
题意:某公司每5个月公布一次财务报告,一年公布了8次,每一次都是亏损,但是不知道每一次亏损了多少,题目给定了每个月的盈利值s和亏损值d,问,这一年能否不亏损,如果能,最大盈利值是多少?
思路:一开始没有懂什么意思,后来师父解释如下:1~5,2~6,3~7,4~8,5~9,...8~12.共8个季度,每个季度都是亏钱,要求最大的盈利值,就要使全年亏钱的月份最少,盈利月份数最多,并且满足每个季度都是亏损的,我用的枚举方法有如下情况(从盈利最多的情况开始枚举,直到枚举到全年亏损的情况):
1:每个季度4个月盈利,1个月亏。ssssd
2: 每个季度3个月盈利,2个月亏。sssdd
3: 每个季度2个月盈利,3个月亏。ssddd
4:每个季度1个月盈利,4个月亏。sdddd
5: 每个季度都亏损。输出 :Deficit。
#include<stdio.h> int main() { int s,d;//s是赚的钱,d是亏的钱 while(scanf("%d%d",&s,&d)!=EOF) { if(4*s < d&&(10*s-2*d>=0))//每个季度里4个月赚,1个月亏。全年10个月赚,2个月亏。 printf("%d\n",10*s-2*d); else if(3*s < 2*d&&(8*s-4*d>=0))//每个季度3个月赚,2个月亏。之后以此类推。 printf("%d\n",8*s-4*d); else if(2*s < 3*d&&(6*s-6*d>=0)) printf("%d\n",6*s-6*d); else if(s < 4*d&&(3*s-9*d>=0)) printf("%d\n",3*s-9*d); else printf("Deficit\n");//以上条件都不满足,说明全年只有亏损 } return 0; }
思路2:在师父吐槽了我的方法的局限性后,他给我解释了一下他的方法,可以适用更大数据的情况,解释了半天,我都不知道怎么用代码去实现,最后师父亲自写出来我才懂了,泪目~~
全年每个月的盈利情况如下: 1:ssssdssssdss
2:sssddsssddss
3:ssdddssdddss
4:sddddsddddsd
5:dddddddddddd
#include<stdio.h> int main() { int s,d,i,n,sum;//s是赚的钱,d是亏的钱 while(scanf("%d%d",&s,&d)!=EOF) { n = 5; for(i = 4; i >= 0; i --)//每个季度盈利月份从4枚举到0 { if(i*s < (n-i)*d) break; }//找到满足每个季度亏损并且使全年盈利最多的月份数i sum = (i*s-(n-i)*d)*2;//计算在前10个月总的盈利数 if(i > 2) sum += s*2;//当每个季度的盈利月份大于2时,全年剩余的两个月也是盈利 else sum += s*i - d*(2-i);//反之,有可能一个月亏或者最后的两个月都亏损 if(sum >= 0) printf("%d\n",sum); else printf("Deficit\n"); } return 0; }
以上是关于poj 2586 Y2K Accounting Bug贪心刷题计划的主要内容,如果未能解决你的问题,请参考以下文章
POJ - 2586 Y2K Accounting Bug (找规律)