10/14程式
unknown
plain_text
3 years ago
5.1 kB
20
Indexable
#include<iostream>
using namespace std;
int main()
{
int type = 0; //車輛的種類
int stationNum = 0; //場站數量
float RateDecline = 0; //在場站本身的稼動率下降程度
float firstRateDecline = 0; //距離場站 1 到 300 公尺的稼動率下降程度
float secondRateDecline = 0; //距離場站 301 到 500 公尺的稼動率下降程度
float thirdRateDecline = 0; //距離場站 501 到 1000公尺 的稼動率下降程度
bool check = true;
int* charge = new int [type]; //各車型的每小時收費
int* carNum = new int [type]; //各車型的車輛數
int* currentstation = new int [stationNum];
int* stationMax = new int [stationNum]; //各場站的車輛最大負荷量
int** distance = new int* [stationNum]; //各場站間的距離
double** expectRate = new double* [type]; //代表各車種在各場站的預期稼動率
int** output = new int* [type];
cin >> type >> stationNum >> RateDecline >> firstRateDecline >> secondRateDecline >> thirdRateDecline;
for(int i = 0; i < type; i++)
{
cin >> charge[i];
}
for(int i = 0; i < type; i++)
{
cin >> carNum[i];
}
for(int i = 0; i < stationNum; i++)
{
currentstation[i] = 0;
cin >> stationMax[i];
}
for(int i = 0; i < type; i++)
{
expectRate[i] = new double [stationNum];
for(int j = 0; j < stationNum; j++)
{
cin >> expectRate[i][j];
}
}
for(int i = 0; i < stationNum; i++)
{
distance[i] = new int [stationNum];
for(int j = 0; j < stationNum; j++)
{
cin >> distance[i][j];
}
}
for(int i = 0; i < type; i++) //初始化要輸出的動態陣列,稍後計算時要存入車輛放在各場站的情形
{
output[i] = new int [stationNum];
for(int j = 0; j < stationNum; j++)
{
output[i][j] = 0;
}
}
int revenue = 0;
int maxvalue = 0;
int cartype = 0;
int station = 0;
while(true)
{
for(int i = 0; i < type; i++)
{
for(int j = 0; j < stationNum; j++)
{
int revenue = 0; //用於計算放各種車在各場站時會造成的收入以及虧損
int decline = 0;
if(currentstation[j] != stationMax[j]) //如果沒有超過該場占最大限制的話,才進行運算
{
revenue = charge[i]* expectRate[i][j]* 24; //運算投放在該場站增加的收入
for(int k = 0; k < type; k++) //開始運算其他場站原有汽車的稼動率下降導致的虧損
{
for(int m = 0; m < stationNum; m++)
{
if(distance[j][m] == 0) //如果是影響到本站的車的話, 用本站的計算方式
{
decline += output[j][m]* RateDecline* 24;
cout<<"CHECK 0"<<decline<<endl;
}
else if(0 < distance[j][m] < 301) //如果是影響到第一層的車,用第一層的計算方式
{
decline += output[j][m]* firstRateDecline* 24;
cout<<"CHECK 1"<<decline<<endl;
}
else if(300 < distance[j][m] < 501) //如果是影響到第二層的車,用第二層的計算方式
{
decline += output[j][m]* secondRateDecline* 24;
cout<<"CHECK 2"<<decline<<endl;
}
else if(500 < distance[j][m] < 1001) //如果是影響到第三層的車,用第三層的計算方式
{
decline += output[j][m]* thirdRateDecline* 24;
cout<< "CHECK 3" <<decline <<endl;
}
}
cout<<"CHECKDECLINE"<<decline<<endl;
}
if(revenue - decline > maxvalue) //如果收入減虧損大於最大收益的話記錄下該投放的車種編號,以及投放場站的編號
{
maxvalue = revenue - decline;
cartype = i;
station = j;
cout << "MAXVALUE"<< maxvalue << endl;
}
}
}
}
for(int i = 0; i < stationNum; i++) //檢查每一個場站的現狀,避免超過最大限制
{
if(i == station)
{
currentstation[i] ++;
}
}
if(maxvalue > 0) //如果有最大收益的話,把庫存的車種減一,並把場站的車輛數加一
{
carNum[cartype] --;
output[cartype][station] ++;
currentstation[station] ++;
maxvalue = 0; //將最大收益歸零,以重新計算
}
else if(maxvalue == 0) //如果找不到最大收益了,就將 check 改為 false 結束 while 迴圈
{
break;
}
}
for(int i = 0; i < type; i++) //最後輸出各車種在各場站投放量
{
for(int j = 0; j < stationNum; j++)
{
if(j == stationNum - 1)
{
cout << output[i][j] << endl;
}
else
{
cout << output[i][j] << ",";
}
}
}
//清除動態陣列空間
for(int i = 0; i < type; i++)
{
delete[] expectRate[i];
}
delete[] expectRate;
delete[] charge;
delete[] carNum;
delete[] stationMax;
charge = carNum = stationMax = nullptr;
expectRate = nullptr;
return 0;
} Editor is loading...