杭电ACM1013

(1)题意:

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

(4)代码:

#include<iostream>

#include<cstring>

using namespace std;

int main()

{

 int surplus, deficit, month[12];

 while (cin >> surplus >> deficit)

 {

  memset(month, 0, sizeof(month));//surpuls:0  deficit:1

  int counts = 0, countd = 0;

  for (int i = 0;i<8;i++)

  {

   while (1)

   {

    countd = month[i] + month[i + 1] + month[i + 2] +

     month[i + 3] + month[i + 4];

    counts = 5 - countd;

    if (counts*surplus >= countd*deficit)

    {

     for (int j = i + 4;j >= 0;j--)

     {

      if (month[j] != 1)

      {

       month[j] = 1;

       break;

      }

     }

    }

    else

     break;

   }

  }

  countd = 0;

  for (int i = 0;i<12;i++)

  {

   countd += month[i];

  }

  counts = 12 - countd;

  int ans = counts*surplus - countd*deficit;

  if (ans>0)

   cout << ans << endl;

  else

   cout << "Deficit" << endl;

 }

 return 0;

}
(3)感想:
8次的循环,五个月放在一起看,在满足五个月的总收入为负的情况下尽可能增加五个月中盈余的月份,可以将初始全部设为盈余, 因为区间是向后重叠的,所以尽量把亏损的月份放在后面,把五个月从后往前的设为亏损直到总收入为负
MS公司每月要么盈余,要么亏损,且一年中每个月的盈余是一样的,亏损也是一样的。
财务统计是每五个月统计一次收入总额,即1-5,2-6……8-12,共有8次统计结果,并且这八次结果公司都亏损的。
给出surplus和deficit,求出全年最大的盈余数

你可能感兴趣的:(杭电ACM1013)