题目来源:http://poj.org/problem?id=2586
Y2K Accounting Bug
Time Limit: 1000MS | Memory Limit: 65536K |
Total Submissions: 17015 | Accepted: 8587 |
Description
Accounting for Computer Machinists (ACM) hassufferred from the Y2K bug and lost some vital data for preparing annual reportfor MS Inc.
All what they remember is that MS Inc. posted a surplus or a deficit each monthof 1999 and each month when MS Inc. posted surplus, the amount of surplus was sand each month when MS Inc. posted deficit, the deficit was d. They do notremember which or how many months posted surplus or deficit. MS Inc., unlikeother companies, posts their earnings for each consecutive 5 months during ayear. ACM knows that each of these 8 postings reported a deficit but they donot know how much. The chief accountant is almost sure that MS Inc. was aboutto 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 surplusthat they can post.
Input
Input is a sequence of lines, each containingtwo positive integers s and d.
Output
For each line of input, output one linecontaining either a single integer giving the amount of surplus for the entireyear, or output Deficit if it is impossible.
Sample Input
59 237
375 743
200000 849694
2500000 8000000
SampleOutput
116
28
300612
Deficit
Source
-----------------------------------------------------
解题思路
数学题,题目的中文大意:
MS公司1999年的账目数据被病毒损坏了,现在只知道MS公司每个月如果盈利的话,盈利都是整数s, 如果亏损的话,亏损都是整数d. 另外知道MS公司1999年的所有8份5个连续月的报表都是亏损的。问在给定的(s,d)下MS公司1999年全年是否可能盈利,如果可能,最大可能盈利是多少?
-----------------------------------------------------
代码
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
#ifndef ONLINE_JUDGE
ifstream fin("2586.txt");
int s,d,ans;
while (fin >> s >> d)
{
ans = -1;
if (s*4<d)
{
ans = s*10-d*2;
}
else if (3*s<2*d)
{
ans = s*8-d*4;
}
else if (2*s<3*d)
{
ans = 6*s-6*d;
}
else if (s<4*d)
{
ans = 3*s-9*d;
}
if (ans<0)
{
cout << "Deficit" << endl;
}
else
{
cout << ans << endl;
}
}
fin.close();
#endif
#ifdef ONLINE_JUDGE
int s,d,ans;
while (cin >> s >> d)
{
ans = -1;
if (s*4<d)
{
ans = s*10-d*2;
}
else if (3*s<2*d)
{
ans = s*8-d*4;
}
else if (2*s<3*d)
{
ans = 6*s-6*d;
}
else if (s<4*d)
{
ans = 3*s-9*d;
}
if (ans<0)
{
cout << "Deficit" << endl;
}
else
{
cout << ans << endl;
}
}
#endif
}