开发者

vector function implement the greedy algorithm but answer have some wrong, some result can't print out [closed]

Closed. This question needs debugging details. It is not currently accepting answers.

Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.

Closed 1 hour ago.

Improve this question

Please refer the figure and modify the code. My answer have some wrong, some parcel will disappear. Please help to make some modification.

SAMPLE INPUT:

  • Maximum amount of weight a truck is allowed to carry
  • parcel arriving at trucking station(in sequence)
  • weight of each parcel arriving at the loading station

SAMPLE OUTPUT:

  • trucks leaving the station(in sequence) and the corresponding parcels and weight that has been loaded into the truck
  • parcels that could not be loaded into the truck and the corresponding weight of the parcel.
cout <<"---------------------------------------------------------------" <<endl;
cout <<"Enter Maximum Amount of weight(KG) a truck is allowed to carry." << endl;
cout <<"Maximum Amount: ";
cin>>WT; //the maximum amount of weight
cout <<"Enter the number of parcels wants to ship." << endl;
cout <<"Number of parcels:";
cin>>num_parcel; //total number of parcels

vector<int>parcel_weight(num_parcel,0); //create the vector weight of parcel array
for(int i=0;i<num_parcel;i++) //input weight of every parcel
{
    cout << "No." << i+1 << " Parcel weight: ";
    cin>>parcel_weight[i];
}

vector<vector<pair<int,int>>>truck(5,vector<pair<int,int>>()); //create 5 truck array & put the parcel weight
vector<pair<int,int>> unloaded_parcel; //unloaded parcel vector array
int truck_No=0;
int total_weight_truck=0;

for(int i=0;i<num_parcel;i++) //check the parcel and separately the parcel to relative array.
{
    if(parcel_weight[i]>WT || truck_No>=5) //if parcel can't loaded
    {
        unloaded_parcel.push_back(make_pair(i,parcel_weight[i]));//put parcel to unloaded_parcel array, if exceed truck can carry weight.
    }
    else if(parcel_weight[i]> WT-total_weight_truck)  //if parcel exceed the truck weight, move to the next truck.
    {
        total_weight_truck=0; //new truck
        truck_No++;
    }
    else //parcel load
    {
        total_weight_truck= total_weight_truck+parcel_weight[i];
        truck[truck_No].push_back(make_pair(i,parcel_weight[i]));
    }
}

for(int i=0;i<5;i++) //print out the 5 truck information which include the parcel
{
    cout<< "Truck " << i+1 << " included:" << endl;
    for(int j=0;j<truck[i].size();j++)
    {
        cout<<"Parcel No." << truck[i][j].first+1 <<" and parcel weight is " <<truck[i][j].second<< 开发者_运维问答"KG" <<endl;
    }
    cout<<endl;
}

cout<<"Parcel can't loaded in truck is:" << endl; //print out the parcel that can't load
for(int i=0;i<unloaded_parcel.size();i++)
{
    cout<<"Parcel No."<<unloaded_parcel[i].first+1<< " and parcel weight is "<<unloaded_parcel[i].second<< "KG" <<endl;
}

Here is my complier answer

vector function implement the greedy algorithm but answer have some wrong, some result can't print out [closed]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜