【发布时间】: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<Solution>中,您就可以开始按成本排序或找到最快的解决方案。 -
是的,我们知道,但问题是我们如何在其中编写代码?至少可以给出一个示例代码吗?我们得到了我们只是不确定如何将其执行到代码中的逻辑。
-
但是我们确实尝试使用向量
但是infile“>>”这部分出错了。有没有办法在文件访问中使用矢量? -
这是可能的,但通常不这样做。您不需要它来完成这项作业。
标签: c++ visual-c++ c++17