POJ 2856 Y2K Accounting Bug

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
描述

计算机机械师会计师事务所(ACM)遭遇了Y2K错误,并且为MS公司准备的年度报告丢失了一些重要数据。他们记得的是,MS 在1999年每个月都会发布盈余或亏损,每个月都会出现盈余或赤字。每个月MS公司公布盈余,金额为:s,每个月MS公司发布亏损,赤字为:d。他们不记得过剩或多少月的盈余或赤字。与其他公司不同,MS Inc.在一年内连续5个月公布其收益。ACM知道这8个帖子中的每一个都报告了赤字,但他们不知道多少。总会计师几乎可以肯定,MS Inc.即将在1999年全年盈余,几乎但并不完全。

编写一个程序,决定MS公司在1999年是否出现亏损,或者如果1999年有盈余,那么他们可以发布的最大盈余是多少。

输入
输入是一系列行,每行包含两个正整数s和d。

输出
对于每行输入,输出一行包含一个整数,给出全年的剩余量,如果不可能,则输出Deficit。

样本输入

59 237
375 743
200000 849694
2500000 8000000

样本输出

116
28
300612
Deficit

分析:
在一年中这样的报表总共有8次(1到5,2到6,…,8到12),现在要确定当赢s和亏d给出,并满足每张报表为亏的情况下,全年公司最高可赢利多少,若存在,则输出有多少盈利金额,若不存在,输出"Deficit"。
在保证连续5个月都亏损的前提下,使得每5个月中亏损的月数最少。
x=1: ssssd,ssssd,ss d>4s 赢利10个月 10s-2d
x=2: sssdd,sssdd,ss 2d>3s 赢利8个月 8s-4d
x=3: ssddd,ssddd,ss 3d>2s 赢利6个月 6s-6d
x=4: sdddd,sdddd,sd 4d>s 赢利3个月 3s-9d
x=5: ddddd,ddddd,dd 无赢利

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in); 
		int s,d,sNum, dNum; 
		while(sc.hasNextInt()) {    //判断输入是否为数字
			s=sc.nextInt();
			d=sc.nextInt();
			if (d > 4*s)
		     {
		         sNum = 10;
		         dNum = 2;
		     }
		     else if (2*d > 3*s)
		     {
		         sNum = 8;
		         dNum = 4;
		     }
		     else if (3*d > 2*s)
		     {
		         sNum = 6;
		         dNum = 6;
		     }
		     else if (4*d > s)
		     {
		         sNum = 3;
		         dNum = 9;
		     }
		     else 
		     {
		         sNum = 0;
		         dNum = 12;
		     }
		     int sum = sNum*s - dNum*d;
		      if(sum>0)
		    		System.out.println(sum); 
			   else 
					System.out.println("Deficit"); 
		}	
	}
}

感觉题意有点难看懂和理解,考验数学思维。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值