TopCoder算法竞赛题6:SRM 150 DIV 2, 250-point

Problem Statement

When a widget breaks, it is sent to the widget repair shop, which is capable of repairing at most numPerDay widgets per day. Given a record of the number of widgets that arrive at the shop each morning, your task is to determine how many days the shop must operate to repair all the widgets, not counting any days the shop spends entirely idle.

For example, suppose the shop is capable of repairing at most 8 widgets per day, and over a stretch of 5 days, it receives 10, 0, 0, 4, and 20 widgets, respectively. The shop would operate on days 1 and 2, sit idle on day 3, and operate again on days 4 through 7. In total, the shop would operate for 6 days to repair all the widgets.

Create a class WidgetRepairs containing a method days that takes a sequence of arrival counts arrivals (of type vector <int>) and an int numPerDay, and calculates the number of days of operation.

 

Definition

Class: WidgetRepairs

Method: days

Parameters: vector <int>, int

Returns: int

Method signature: int days(vector <int> arrivals, int numPerDay)

(be sure your method is public)

    

 

Constraints

arrivals contains between 1 and 20 elements, inclusive.

Each element of arrivals is between 0 and 100, inclusive.

numPerDay is between 1 and 50, inclusive.

 

Examples

0)

{ 10, 0, 0, 4, 20 }

8

Returns: 6

 

1)

{ 0, 0, 0 }

10

Returns: 0

 

2)

{ 100, 100 }

10

Returns: 20

 

3)

{ 27, 0, 0, 0, 0, 9 }

9

Returns: 4

 

4)

{ 6, 5, 4, 3, 2, 1, 0, 0, 1, 2, 3, 4, 5, 6 }

3

Returns: 15

 

Source code

#include <iostream>

#include <vector>

using namespace std;

 

class WidgetRepairs

{

public:

      int days(vector <int> arrivals, int numPerDay)

      {

           int rest = 0;

           int num = numPerDay;

           int count = 0;

           for(int i=0; i<arrivals.size(); i++)

           {

                 rest += arrivals[i];

                 if(rest > 0)

                 {

                      count++;

                      rest = rest>num?(rest-num):0;

                 }

           }

           while(rest>0)

           {

                 count++;

                 rest = rest>num?(rest-num):0;

           }

           return count;

      }

};

 

void main()

{

      WidgetRepairs w;

 

//  测试数组一

//   int a[] = {10, 0, 0, 4, 20};

//   vector <int> data(a, a+5);

//   cout<<w.days(data, 8)<<endl;

 

//  测试数组二

//   int a[] = {100, 100};

//   vector <int> data(a, a+2);

//   cout<<w.days(data, 10)<<endl;

 

//  测试数组三

//   int a[] = {27, 0, 0, 0, 9};

//   vector <int> data(a, a+5);

//   cout<<w.days(data, 9)<<endl;

 

//  测试数组四

      int a[] = {6, 5, 4, 3, 2, 1, 0, 0, 1, 2, 3, 4, 5, 6};

      vector <int> data(a, a+14);

      cout<<w.days(data, 3)<<endl;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值