根据可用空间用另一个数组中的项目填充数组

Filling an array with items from another array based on available space

本文关键字:数组 项目 填充 另一个 空间      更新时间:2023-10-16

如何将每辆卡车的"装载空间"(二维阵列的第二部分(限制为 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存在电流回路并到达下一辆卡车。