【问题标题】:Cargo transportation system for loop problem循环问题的货物运输系统
【发布时间】:2020-08-13 12:34:50
【问题描述】:

enter image description here 这有问题的所有细节

我们如何打印选项的数量 我们使用这个公式options = (cargo / lorrysize) + 1 这给了我们选项的数量 但在那之后我们被困住了 示例货物尺寸为 100

100     100/30=3+1=4
option 1
30
30
30
option 2
30
30
10
10
10
option 3
10x10
option 4
30
10x7

第二个例子

150      150/30=5+1=6
option 1
30x5
option 2
30x4
10x3
option 3
30x3
10x6
option 4
30x2
10x9
option 5
30
10x12
option 6
10x15

我们尝试这样做,但不知道如何编写“for 循环”部分 我希望这足以让你们理解

#include <iostream>
#include <vector>
#include <iterator>
#include <fstream>
#include<string>
using namespace std;

/*
Steps to complete program
Step 1: Find total options available (how many Lorrys & Vans) for the input cargo capacity in .txt files
Step 2: For each option, find its:   Amount of Lorrys and Vans
                                     Total cost for both the Lorrys and Vans
                                     Find the max trip
Step 3: Find the top 3 cheapest/lowest cost trips and display
Step 4: Find the fastest available trip and display
*/

class CTS //cargo transport system
{
int i;
    int cargo, lorryprice, vanprice, lorrysize, vansize, allOps;
    //vector<double> options, lorry, vans, totalC, nooftrips;
    //vector <double>::iterator option, minilorry, cost, trips, van;

public:
    void set_cargo(int);
    void set_lorryprice(int);
    void set_vanprice(int);
    void set_lorrysize(int);
    void set_vansize(int);

};
void CTS::set_cargo(int total_cargo){
    cargo = total_cargo;
}
void CTS::set_lorryprice(int lorryP){
    lorryprice = lorryP; 
}

void CTS::set_vanprice(int vanP){
    vanprice = vanP;
}
void CTS::set_lorrysize(int lorryS){
    lorrysize = lorryS;
}
void CTS::set_vansize(int vanS)
{
    vansize = vanS;
}

int main()
{
    int cargo, lorryprice, vanprice, lorrysize, vansize, options, i;
    ifstream infile;
    infile.open("size.txt");
    if(infile.is_open()){
        infile >> cargo;
        infile >> lorryprice;
        infile >> vanprice;
        infile >> lorrysize;
        infile >> vansize;
    }
 CTS run;
    run.set_cargo(cargo);
    run.set_lorryprice(lorryprice);
    run.set_vanprice(vanprice);
    run.set_lorrysize(lorrysize);
    run.set_vansize(vansize);

    infile.close();

    options = (cargo / lorrysize) + 1;
    
    
    for (i = 0; i < options; i++)
    {
       

        cout << i << " " << cargo - lorrysize << endl; // this part we need help with we have no clue




    }
    


    /*cout << cargo << endl;
    cout << lorryprice << endl;
    cout << vanprice << endl;
    cout << lorrysize << endl;
    cout << vansize << endl;*/
    return 0;
}

【问题讨论】:

  • 是的,您发布的公式为您提供了卡车旅行的数量(例如 4L 0V)。现在您必须考虑如果您使用一辆或多辆货车执行其中一次卡车旅行会发生什么。 (3L 1V)甚至1L 8V,如表中的赋值。生成所有这些场景后,您可以计算成本并按成本对结果列表进行排序。
  • 至于实现,考虑一个Solution 类或结构,它记录了您需要多少辆货车和货车,以及成本和行程数量的计算。将您的所有解决方案存储在 std::vector&lt;Solution&gt; 中,您就可以开始按成本排序或找到最快的解决方案。
  • 是的,我们知道,但问题是我们如何在其中编写代码?至少可以给出一个示例代码吗?我们得到了我们只是不确定如何将其执行到代码中的逻辑。
  • 但是我们确实尝试使用向量 但是infile“>>”这部分出错了。有没有办法在文件访问中使用矢量?
  • 这是可能的,但通常不这样做。您不需要它来完成这项作业。

标签: c++ visual-c++ c++17


【解决方案1】:

一旦您拥有最大数量的卡车,您可以减去卡车的货物并计算剩余的货车数量,如下所示:

for (int num_lorries = 0; num_lorries < options; num_lorries++) {
    int remaining_cargo = cargo - lorrysize * num_lorries;
    int num_vans = remaining_cargo / vansize;
    // Exercise for you: figure out why this bit is needed.
    // (consider remaining_cargo=25 and vansize = 10)
    if (remaining_cargo % vansize > 0) {
        num_vans++;
    }
    Solution sol(num_lorries, num_vans);
    // Do something with sol
}

【讨论】:

    猜你喜欢
    • 2014-11-27
    • 1970-01-01
    • 2011-11-08
    • 2013-11-25
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多