POJ - 2586

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

翻译

计算机机工会计(ACM)受到了千年虫问题的困扰,失去了一些为MS公司编写年度报告的重要数据。他们所记得的是,MS Inc.每个月和MS Inc.公布盈余的每个月都会出现盈余或亏损,盈余的数量是s,而MS Inc.公布的每个月的亏损都是d。他们不记得哪个月或多少个月出现盈余或亏损。与其他公司不同的是,MsInc.在一年中连续5个月公布收入。ACM知道,这8篇文章中的每一篇都报告了亏损,但他们不知道有多少。总会计师几乎可以肯定,MS公司将在1999年全年出现盈余。差不多但不完全是。编写一个程序,以决定MS Inc.在1999年期间是否有亏损,或者如果1999年有可能出现盈余,他们可以公布的最大盈余是多少。
题意:公司丢失了年度报告的数据,他们记得的是,每个月都会出现盈利或亏损,盈利的数量是S,亏损的数量是d,但是他们不记得哪个月或多少个月出现盈利或亏损。公司在一年中连续5个月公布收入。ACM知道,这8篇报告都出现了亏损,然后判断1999年是否出现了盈利,若出现,则最大盈利是多少。

题意

ACM丢失了1999年的报告数据,他们每连着五个月会进行一次盈亏报告(1-5,2-6,3-7,4-8,5-9,6-10,7-11,8-12),每个月盈利都是s,亏损都是d,ACM不知道他们每个月盈利和亏损了多少s或d,,但是他们知道他们8次都是亏损,请写出程序看MS公司这一年是否亏本,或者有可能盈利的最大值。

思路

在保证连续5个月都亏损的前提下,使得每5个月中亏损的月数最少,则有5种情况。
当x = 1: ssssd, ssssd, ss ; d > 4s 盈利了10个月;
当x = 2: sssdd, sssdd, ss ; 2d > 3s 盈利了8个月;
当x = 3: ssddd, ssddd, ss ; 3d > 2s 盈利了6个月;
当x = 4: sdddd, sdddd, sd ; 4d > s 盈利了3个月;
当x = 5: ddddd, ddddd, dd ; 亏损

涉及的算法

贪心;

AC代码

#include<stdio.h>
int main ()
{
	long long s, d;
	long long sum;
	while(~scanf("%lld%lld",&s, &d))
	{
		if(d > 4*s)
			sum = 10*s - 2*d;
		else if(2*d > 3*s)
			sum = 8*s - 4*d;
		else if(3*d > 2*s)
			sum = 6*s - 6*d;
		else if(4*d > s)
			sum = 3*s - 9*d;
		else
			sum = -1;
		if(sum < 0)
			printf("Deficit\n");
		else
			printf("%ld\n",sum);
	}
}

//这题主要是搞懂题目意思,搞懂之后就很简单。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值