查找C ++中每个辐射站的最终辐射
假设直线上有N个测站。它们各自具有相同的辐射功率的非负功率。每个站点可以通过以下方式增加其相邻站点的辐射功率。
假设具有辐射功率R的第i个站点,将第(i–1)个站点的辐射功率增加R-1,第(i-2)个站点的辐射功率增加R-2,并将第(i+1)个站点的辐射功率增加R-1的辐射功率,R-2的第(i+2)站的辐射功率。等等。因此,例如,如果数组的值类似于Arr=[1、2、3],则输出将为3、4、4。新辐射将为[1+(2-1–)+(3-2),2+(1–1)+(3-1),3+(2–1)]=[3,4,4]
这个想法很简单。对于每个站,如上,直到有效辐射变为负时,i都会增加相邻站的辐射。
示例
#include <iostream>
using namespace std;
class pump {
public:
int petrol;
int distance;
};
int findStartIndex(pump pumpQueue[], int n) {
int start_point = 0;
int end_point = 1;
int curr_petrol = pumpQueue[start_point].petrol - pumpQueue[start_point].distance;
while (end_point != start_point || curr_petrol < 0) {
while (curr_petrol < 0 && start_point != end_point) {
curr_petrol -= pumpQueue[start_point].petrol - pumpQueue[start_point].distance;
start_point = (start_point + 1) % n;
if (start_point == 0)
return -1;
}
curr_petrol += pumpQueue[end_point].petrol - pumpQueue[end_point].distance;
end_point = (end_point + 1) % n;
}
return start_point;
}
int main() {
pump PumpArray[] = {{4, 6}, {6, 5}, {7, 3}, {4, 5}};
int n = sizeof(PumpArray)/sizeof(PumpArray[0]);
int start = findStartIndex(PumpArray, n);
if(start == -1)
cout<<"No solution";
else
cout<<"Index of first petrol pump : "<<start;
}输出结果
Index of first petrol pump : 1
热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿