根据可用空间

时间:2016-02-14 20:19:01

标签: c++ arrays pointers multidimensional-array

如何将每辆卡车的“装载空间”(2D阵列的第2部分)限制为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 << "\n\nShuffled Packages: \n";
    printArray(iPackages, numPackages);

    int** iTrucks = createTrucks(numTrucks);
    cout << "\n\nInitial 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);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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存在当前循环并进入下一辆卡车。