poj 2586--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.
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
每年统计八次,每次五个月,即1-5 6-10 7-11。。每次都是亏损,问年收益情况
我的想法是因为1-5 6-10都是亏损,未知的是11 12
要想最后的收益最大,那么每个月的亏损必须最小,即y=dx-s(5-x)<0时取最大值,即x=5s/(s+d)+1.
下面要考虑的是11
12月份,因为这两个月的情况有三种,正正,正负,或是负负,正正的时候即当x<4时可以满足,正负时说明x=4,若是负负,则必定亏损,不需要考虑。
View Code
1 #include <stdio.h> 2 #include <string.h> 3 #include <math.h> 4 int main() 5 { 6 int s,d; 7 while(scanf("%d%d",&s,&d)!=EOF) 8 { 9 int x=5*s/(s+d)+1; 10 if(x<4) 11 x=(-d*x+s*(5-x))*2+s*2; 12 else 13 { 14 if(x==4) 15 x=3*s-9*d; 16 else 17 x=-1; 18 } 19 if(x<0) 20 printf("Deficit\n"); 21 else 22 printf("%d\n",x); 23 } 24 return 0; 25 }