## 程式碼一 ```{c++} #include<iostream> using namespace std; int main() { const long ARRAY_SIZE = 101; long target_amount = 0; long inventory_cost = 0; long shortage_cost = 0; long init_storage = 0; long length_of_day = 0; long minma_sum_cost = 10000; long minma_s0 = 0; long result = 0; long inventory[ARRAY_SIZE] = {0}; long demand[ARRAY_SIZE] = {0}; long backup[ARRAY_SIZE] = {0}; long sum_cost = 0; cin >> target_amount >> inventory_cost >> shortage_cost >> init_storage; cin >> length_of_day; inventory[0] = init_storage; for (long i = 1; i <= length_of_day; i++) { long a_value = 0; cin >> a_value; demand[i] = a_value; backup[i] = a_value; } demand[0] = 0; for (long threshold = 0; threshold < target_amount; threshold++) { for (long i = 1; i <= length_of_day ; i++) { demand[i] = backup[i]; } for (long i = 0; i < ARRAY_SIZE; i++) { inventory[i] = 0; } inventory[0] = init_storage; sum_cost = 0; for (long i = 1; i <= length_of_day; i++) { long inventory_at_night = 0; inventory_at_night = inventory[i - 1] - demand[i]; result = 0; if (inventory_at_night >= 0) { result = inventory_at_night * inventory_cost; } else { result = inventory_at_night * shortage_cost * -1; } sum_cost += result; if (inventory_at_night <= threshold) { inventory[i] = target_amount; } else { inventory[i] = inventory_at_night; } } if (minma_sum_cost > sum_cost) { minma_sum_cost = sum_cost; minma_s0 = threshold; } } cout << minma_s0 << "," << minma_sum_cost; } ``` ## 程式碼二 ```{c++} #include<iostream> using namespace std; int main(){ const int ARRAY_SIZE = 101; int target_amount = 0; int inventory_cost = 0; int shortage_cost = 0; int init_storage = 0; int length_of_day = 0; int minma_sum_cost = 10000; int minma_s0[101] = {0}; int qulifiy_s0[101] = {0}; int result = 0; int inventory[ARRAY_SIZE] = {0}; int demand[ARRAY_SIZE] = {0} ; int backup[ARRAY_SIZE] ={0}; int timer = 0; cin >> target_amount >> inventory_cost >> shortage_cost >> init_storage; cin >> length_of_day; inventory[0] = init_storage; for (int i = 1; i <= length_of_day; i++){ int avalue = 0; cin >> avalue; demand[i] = avalue; backup[i] = avalue; } for (int threshold = 0;threshold < target_amount; threshold++){ for(int i = 1; i<=length_of_day+1;i++){ demand[i] = backup[i]; } for (int i = 0; i < 101; i++){ inventory[i] = 0; } inventory[0] = init_storage; int sum_cost = 0; for (int i = 1; i <= length_of_day; i++){ int inventory_at_night; inventory_at_night = inventory[i-1] - demand[i]; result = 0; if (inventory_at_night >= 0){ result = inventory_at_night * inventory_cost; }else{ result = inventory_at_night * shortage_cost * -1; } sum_cost += result; if (inventory_at_night <= threshold){ inventory[i] = target_amount; }else{ inventory[i] = inventory_at_night; } } if (minma_sum_cost > sum_cost){ minma_sum_cost = sum_cost; } minma_s0[threshold] = sum_cost; cout << sum_cost << "," << threshold << "\n"; } for (int i = 0 ;i < target_amount;i++) { if(minma_s0[i] == minma_sum_cost){ qulifiy_s0[timer] = i; timer++; } } for (int i = 0; i < timer; i++){ cout << qulifiy_s0[i] << "," << minma_sum_cost; if (timer - i != 1){ cout << ","; } } cout << endl; return 0; } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up