根据可用空间用另一个数组中的项目填充数组
Filling an array with items from another array based on available space
如何将每辆卡车的"装载空间"(二维阵列的第二部分(限制为 1000?我在loadTrucks函数中的do-while循环无法按预期工作。我尝试对数组求和并使用各种组合的 do-while、a while和 if/else 语句来限制它,但无济于事。任何帮助或建议将不胜感激。谢谢你的时间。
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;
int* initializePackages(int &numPackages);
void printArray(int* arr, int size);
void shufflePackages(int* packages, int size);
int** createTrucks(int &numTrucks);
void printTrucks(int** arr, int size);
void loadTrucks(int** trucks, int* packages, int numTrucks, int numPackages);
int main()
{
int numPackages; //receives the passed in values from initializePackages.
int numTrucks; //receives the passed in values from createTrucks.
srand(time(0)); //Seed the random function with the time clock.
int* iPackages = initializePackages(numPackages);
cout << "nSorted Packages: n";
printArray(iPackages, numPackages);
shufflePackages(iPackages, numPackages);
cout << "nnShuffled Packages: n";
printArray(iPackages, numPackages);
int** iTrucks = createTrucks(numTrucks);
cout << "nnInitial Trucks: n";
printTrucks(iTrucks, numTrucks);
cout << "nLoaded Trucks: n";
loadTrucks(iTrucks, iPackages, numTrucks, numPackages);
printTrucks(iTrucks, numTrucks);
}
int* initializePackages(int &numPackages)
{
int* packages = 0; //returning the pointer from initializePackages.
numPackages = 0; //to pass the number of packages to main.
cout << "Enter the number of packages to be loaded (between 60 and 500): " << endl;
while(numPackages < 60 || numPackages > 500){
cin >> numPackages; //takes in the number of packages to be assigned.
}
packages = new int[numPackages]; //dynamically allocates an array of packages of based on the value of numPackages.
for(int i=0; i < numPackages; i++){ //initializes the array with sequential values specified by numPackages.
*(packages + i) = i +1;
}
return packages;
}
void printArray(int* arr, int size)
{
for(int count = 0; count < size; count ++){
cout << arr[count] << ((count + 1) % 20 ? " " : "n"); //Accesses the array and prints out 20 elements per line.
}
}
void shufflePackages(int* packages, int size)
{
int j; //additional variable to utilize as a swap variable.
int temp; //place holder variable.
for(int i = 0; i < size-1; i++){ //for loop to iterate through the shuffle sequence.
j = rand() % size; //selects random spot in the array to begin shuffling
temp = packages[i]; //assigns a place holder variable to the initial value
packages[i] = packages[j]; //empty packages[i] element is now assigned the value from the packages[j] element
packages[j]=temp; //packages[j] element is now assigned the initial value originally assigned to the place holder variable
}
}
int** createTrucks(int &numTrucks)
{
numTrucks = 0;
cout << "nEnter the number of trucks to be loaded (between 3 and 25): " << endl;
while(numTrucks < 3 || numTrucks > 25){ //specifies the parameters for the number of trucks
cin >> numTrucks;
}
int** trucks = new int* [numTrucks]; //dynamically allocates an array based on number of trucks.
for(int i = 0; i < numTrucks; i++){ //accesses the truck array and assigns a number to each truck.
trucks[i] = new int[20]; //dynamically allocates an array of 20 elements
for(int j = 0; j < 20; j++){ //initializes the truck's available load space with zeros.
trucks[i][j] = 0;
}
}
return trucks;
}
void printTrucks(int** arr, int size)
{
for(int i = 0; i < size; i++){ //loop to print out the trucks and number. "Truck #: "
cout << "Truck " << i + 1 << ": ";
for(int j = 0; j < 20; j++){ //loop prints out the available load space of the truck's trailer.
cout << " " << arr[i][j];
}
cout << "n"; //Starts a new line of truck and load space to be printed.
}
}
void loadTrucks(int** trucks, int* packages, int numTrucks, int numPackages)
{
int sum = 0;
int temp;
for(int i = 0; i < numTrucks; i++){
for(int j = 0; j < 20; j++){
for(int k = 0; k < numPackages; k++){
do{
temp = packages[k];
packages[k] = trucks[i][j];
trucks[i][j] = temp;
sum += trucks[i][j];
}while(sum < 1000);
}
}
}
}
for(int i = 0; i < numTrucks; i++){
for(int j = 0; j < 20; j++){
for(int k = 0; k < numPackages; k++){
if(trucks[i][j] + packages[k] < 1000)
{
trucks[i][j] += packages[k];//adds the package quantity to the truck
sum += trucks[i][j];
}
else break; //gets to the next truck, because the quantity is already filled
}
}
}
如果你想实现的是你所说的,那么你需要这样做。检查卡车中是否有空间,并将当前包裹添加到卡车上。如果卡车已满(1000 +数量或20包裹(,您只需到达下一辆卡车即可。 Break
存在电流回路并到达下一辆卡车。
相关文章:
- 有没有办法使用输入的维度初始化 2d 数组中的所有项目而不循环C++?
- 为什么 C++ 中的字符数组返回其项目而不是地址?
- 数组中无法解释的最后一个项目
- 数组 C++ 中销售最多和最少的项目
- 指向单个项目或数组的指针
- 如何从类中返回自定义数组项目并操纵其属性?C
- 计数器将项目添加到数组中,并剩余
- 如何将类的项目添加到C 的数组中,而无需给它们命名
- 适当的JNI转换将数组项目从jobjectarray到分开const char
- C++检查项目是否在数组中
- C,如何将多维数组传递给CLR/类库项目中的函数
- 将项目插入数组
- C++从指针数组中删除项目
- 根据世界位置查找一维数组中的通用项目
- 在C++中初始化数组时出现 Seg 错误(欧拉项目 3)
- 对包含三个项目的数组进行排序
- 这是一个具有基于数组的列表函数的数据结构项目,我似乎无法摆脱这个无限循环。
- 如何在C++中创建一个返回字节数组的函数?Arduino项目
- 基类的指针数组;每次项目启动我都有不同的结果
- 从庸医(队列/堆栈)中包含的圆形数组顶部弹出一个项目