E - Y2K Accounting Bug

E - Y2K Accounting Bug

题目描述:

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:

在这里插入图片描述

Sample Output:

在这里插入图片描述

题目大意:

这道题特么是坑人的,题目难读懂,讲的是计算机公司丢失了1999年的账单,但是题目输入给出了每个月赤字与盈利的金额,每个月亏损和赚的钱是一定的(这个很重要),然后这个公司发布每个月收入情况是隔五个月来发布的,(所以有8个账单,分别是1-5,2-6,3-7,4-8,5-9,6-10,7-11,8-12,并且每一个账单都是亏损的)这是网上翻译来的,但是,也可以这样理解,每五个月发布一次情况,但都是亏损的情况,所以可以以这样来找出最优解,问该公司在1999年能否有盈利,如果有输出最大盈利,如果没有就输出Deficit。

思路分析:

这道题是很经典的贪心算法,假设s是盈利情况,d是赤字情况,那么可以这样举例子:
ssssdssssdss。(4s<d)
sssddsssddss。(3
s<2d)
ssdddssdddss。(2
s<3d)
sddddsddddsd。(s<4
d)
每五个月 但情况都是亏损的,为什么第四种情况最后2个是s和d,因为可以先从里面按顺序选出五个,才能满足情况,举例(sdddd,dddds,dddsd,ddsdd,dsddd,sdddd,dddds,dddsd)刚好八个账单,并且满足他的条件。

代码块:

#include<stdio.h>
int lemon(int b,int d)
{
	if(4*b<d)
	{
		return 10*b-2*d;
	}
	if(3*b<2*d)
	{
		return 8*b-4*d;
	}
	if(2*b<3*d)
	{
		return 6*b-6*d;
	}
	if(b<4*d)
	{
		return 3*b-9*d;
	}
	return -1;
	
}
int main()
{
	int a,b,c;
	while(scanf("%d %d",&a,&c)!=EOF)
	{
		b=lemon(a,c);
		if(b>0)
		{
			printf("%d\n",b);
		}
		else
		{
		printf("Deficit\n");
	}
	}
}
发布了49 篇原创文章 · 获赞 6 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/xiaosuC/article/details/104123512