1. 问题描述
Suppose that you have been assigned the job of buying the plumbing pipes for a construction project. Your foreman gives you a list of the varying lengths of pipe needed, but the distributor sells stock pipe only in one fixed size. You can, however, cut each stock pipe in any way needed. Your job is to figure out the minimum number of stock pipes required to satisfy the list of requests, thereby saving money and minimizing waste.
Your job is to write a recursive function
int cutStock(Vector & requests, int stockLength);
that takes two arguments—a vector of the lengths needed and the length of stock pipe that the distributor sells—and returns the minimum number of stock pipes needed to service all requests in the vector. For example, if the vector contains [ 4, 3, 4, 1, 7, 8 ] and the stock pipe length is 10, you can purchase three stock pipes and divide them as follows:
Pipe 1: 4, 4, 1
Pipe 2: 3, 7
Pipe 3: 8
Doing so leaves you with two small remnants left over. There are other possible arrangements that also fit into three stock pipes, but it cannot be done with fewer.
2.代码
/*
* File: StockCutting.cpp
* ----------------------
* Name: abdulrhman eaita
* This code minimize the needed stock for a diffrent length requests
*
*/
#include <iostream>
#include "console.h"
#include "vector.h"
#include "simpio.h"
using namespace std;
/* Function prototypes */
int cutStock(Vector<int> & requests, int stockLength);
void bibesCutter(Vector<int> & requests, int stockLength, int &rest, int &bibes);
/* Main program */
int main() {
Vector<int> requests;
while (true) {
while (true) {
int x = getInteger("enter your requests, 0 to end: ");
if(x == 0) break;
requests.add(x);
}
int stoclLen = getInteger("Stock length?: ");
cout << "Minimum number of stock pipes: " << cutStock(requests, stoclLen) << endl;
requests.clear();
}
return 0;
}
/*
* Function: cutStock
* Usage: int units = cutStock(requests, stockLength);
* ---------------------------------------------------
* wrapper function for bibesCutter();
*/
int cutStock(Vector<int> & requests, int stockLength) {
int bibes = 0;
int rest = 0;
bibesCutter(requests,stockLength, rest, bibes);
return bibes;
}
/*
* Function: bibesCutter
* USage: bibesCutter(requests, stocklen, rest, bibes);
* ----------------------------------------------------
* Computes the minimum number of stock pipes required to satisfy
* the vector of requests of individual pieces.
*/
void bibesCutter(Vector<int> & requests, int stockLength, int & rest, int & bibes){
if (requests.size() == 1) {
if (requests[0] > rest) {
bibes++;
//creating new rest as it's new stock and substracting the request after adding new bibe
rest = stockLength - requests[0];
return;
}else{
// substracting the request from current rest only.
rest -= requests[0];
return;
}
}else{
Vector<int> tmp ;
int toCheck = 0;
// checking if there's a smaller value to remove first
for (int i = 0; i < requests.size(); ++i) {
if (requests[i] < rest) {
toCheck = i;
}
}
// removing first item by default, if there's no smaller cut.
tmp.dd(requests[toCheck]);
requests.removeAt(toCheck);
bibesCutter(tmp, stockLength, rest, bibes);
bibesCutter(requests, stockLength, rest, bibes);
return;
}
}
3.算法描述
参量描述
requests: 所需管道的列表
tmp: 需要的管道,但尚未得到分配
toCheck: 满足小于rest的最后一根request, 初始化为0,不存在也为0,准备参与切割
rest: 一根管道被切后剩余长度
bibes: 所需总管道数目步骤
1.初始化,第一次执行bibesCutter
rest=bibes=0
第一次扫描:
temp.add(requests[0])
requests.remove(requests[0])
2.执行bibesCutter(tmp, stockLength, rest, bibes);
执行分割
bibes++; //第一次分割管道
rest=length-tmp;
3.执行bibesCutter(requests, stockLength, rest, bibes);
执行递归