Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C++

Bức xạ cuối cùng của các trạm phát sóng trong C++


Có N trạm trên đường thẳng, cùng công suất bức xạ. Trạm i với công suất R tăng bức xạ trạm i-1 thêm R-1, i-2 thêm R-2, ...; i+1 thêm R-1, i+2 thêm R-2,... Ví dụ [1,2,3] → [3,4,4].

Mỗi trạm tăng cường bức xạ cho các trạm lân cận cho đến khi hiệu ứng âm.

Ví dụ

#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; }

Kết quả

Index of first petrol pump : 1