我目前正在使用一个表格来查找各个油箱中与该船所容纳的燃料总量有关的相应燃料水平。该船目前有8个油箱,燃料的分配与船上运载的总燃料量相对应。给定总燃料量后,我需要找到或填充该总燃料量下8个油箱中的燃料大致位置。
我尝试在给定的时间搜索2d数组以找到相应的燃油箱,但是使用图表(2d数组),我意识到我需要为最左侧的列使用索引(即总燃料) ),然后找到下限行和上限行。然后,我需要找到每个单独元素(燃料箱)两行的平均值。我能够进行if语句和数学运算,但我知道有一种递归方法可以搜索左列,找到给定燃料量的上下键行,并找到每个燃料箱中的量。因此,如果给我目标油量即194568,我需要大约找到8个油箱中的燃料位置。
int main()
const int NUM_ROWS = 22;
const int NUM_COLUMNS = 9;
int arr[NUM_ROWS][NUM_COLUMNS] = {
{80000,4500,7500,11700,11700,0,0,0,9200},
{90000,4500,7500,13800,13800,0,0,0,10800 },
{100000,4500,7500,15900,15900,0,0,0,12400},
{110000,4500,7500,18200,18200,0,0,0,13200},
{120000,4500,7500,20400,20400,0,0,0,14400},
{130000,4500,7500,22000,23000,0,0,0,16000},
{140000,4500,7500,22000,26200,0,0,0,19600},
{150000,4500,7500,22000,29400,0,0,0,23200},
{160000,4500,7500,22000,32700,0,0,0,26600},
{170000,4500,7500,23200,35400,0,0,0,28800},
{180000,4500,7500,25300,37500,0,0,0,30400},
{190000,4500,7500,27400,39600,0,0,0,32000},
{200000,4500,7500,29500,41700,0,0,0,33600},
{210000,4500,7500,31800,44300,0,0,0,33800},
{220000,4500,7500,31800,44300,0,0,10000,33800},
{230000,4500,7500,31800,44300,0,3000,15000,35800},
{240000,4500,7500,31800,44300,0,8000,15000,40800},
{250000,4500,7500,31800,44300,0,8000,25000,40800},
{260000,4500,7500,31800,44300,0,8000,35000,40800},
{270000,4500,7500,31800,44300,0,8000,45000,40800},
{280000,4500,7500,31800,44300,0,14000,45000,44800},
{290000,4500,7500,31800,44300,0,18800,46000,49000}, };
int key;
cout << "Enter Fuel Amount:";
cin >> key;
if (key >= 80000 && key < 90000)
{
cout << "L/R_Ext = " << (arr[0][1] + arr[1][1]) / 2 << endl;
cout << "L/R_ Outbrd = " << (arr[0][2] + arr[1][2]) / 2 << endl;
cout << "L1 R4 = " << (arr[0][3] + arr[1][3]) / 2 << endl;
cout << "L2 R3 = " << (arr[0][4] + arr[1][4]) / 2 << endl;
cout << "Forward = " << (arr[0][5] + arr[1][5]) / 2 << endl;
cout << "Center = " << (arr[0][6] + arr[1][6]) / 2 << endl;
cout << "Mid = " << (arr[0][7] + arr[1][7]) / 2 << endl;
cout << "Aft = " << (arr[0][8] + arr[1][8]) / 2 << endl;
}
else if (key >= 90000 && key < 100000)
{
cout << "L/R_Ext = " << (arr[1][1] + arr[2][1]) / 2 << endl;
cout << "L/R_ Outbrd = " << (arr[1][2] + arr[2][2]) / 2 << endl;
cout << "L1 R4 = " << (arr[1][3] + arr[2][3]) / 2 << endl;
cout << "L2 R3 = " << (arr[1][4] + arr[2][4]) / 2 << endl;
cout << "Forward = " << (arr[1][5] + arr[2][5]) / 2 << endl;
cout << "Center = " << (arr[1][6] + arr[2][6]) / 2 << endl;
cout << "Mid = " << (arr[1][7] + arr[2][7]) / 2 << endl;
cout << "Aft = " << (arr[1][8] + arr[2][8]) / 2 << endl;
}
else if (key >= 100000 && key < 110000)
{
cout << "L/R_Ext = " << (arr[2][1] + arr[3][1]) / 2 << endl;
cout << "L/R_ Outbrd = " << (arr[2][2] + arr[3][2]) / 2 << endl;
cout << "L1 R4 = " << (arr[2][3] + arr[3][3]) / 2 << endl;
cout << "L2 R3 = " << (arr[2][4] + arr[3][4]) / 2 << endl;
cout << "Forward = " << (arr[2][5] + arr[3][5]) / 2 << endl;
cout << "Center = " << (arr[2][6] + arr[3][6]) / 2 << endl;
cout << "Mid = " << (arr[2][7] + arr[3][7]) / 2 << endl;
cout << "Aft = " << (arr[2][8] + arr[3][8]) / 2 << endl;
}
cout << "First row:\n";
for (col = 0; col < NUM_COLUMNS; col++) {
cout << arr[0][col] << '\t';
}
cout << "Second row:\n";
for (col = 0; col < NUM_COLUMNS; col++) {
cout << arr[1][col] << '\t';
}
cout << "\n\nSecond column:\n";
for (row = 0; row < NUM_ROWS; row++) {
cout << arr[row][1] << '\n';
}
cout << "\n\nThird column:\n";
for (row = 0; row < NUM_ROWS; row++) {
cout << arr[row][2] << '\n';
}
cout << "\n\nForth column:\n";
for (row = 0; row < NUM_ROWS; row++) {
cout << arr[row][3] << '\n';
}
/*etc....
我期望给定目标125648时的结果将搜索数组并返回每个油箱中的燃料以获取
int LR_Ext;
int LR_Outbrd;
int L1R4;
int L2R3;
int Forward;
int Center;
int Mid;
int Aft;
,也许将它们分配给变量。
答案 0 :(得分:0)
您可以首先确定一个可以在显示代码中使用的cout
,而不是在每种if
情况下重复整个index
语句:
int index = 0;
if (key >= 80000 && key < 90000)
index = 0;
else if (key >= 90000 && key < 100000)
index = 1;
else if (key >= 100000 && key < 110000)
index = 2;
else
// You should do some check if an invalid key is provided
// Now display
cout << "L/R_Ext = " << (arr[index][1] + arr[index+1][1]) / 2 << endl;
cout << "L/R_ Outbrd = " << (arr[index][2] + arr[index+1][2]) / 2 << endl;
cout << "L1 R4 = " << (arr[index][3] + arr[index+1][3]) / 2 << endl;
cout << "L2 R3 = " << (arr[index][4] + arr[index+1][4]) / 2 << endl;
cout << "Forward = " << (arr[index][5] + arr[index+1][5]) / 2 << endl;
cout << "Center = " << (arr[index][6] + arr[index+1][6]) / 2 << endl;
cout << "Mid = " << (arr[index][7] + arr[index+1][7]) / 2 << endl;
cout << "Aft = " << (arr[index][8] + arr[index+1][8]) / 2 << endl;