這裏的問題是:程序要求優化最小費用
成本分析是工程的一個重要組成部分。在實踐中,可能會要求您編寫程序以確定許多不同潛在環境的最低成本。您的程序可以用作項目的決策工具。
考慮建造一個機場,跑道建在垃圾填埋場。承包商有兩輛自卸車,一輛是8噸,另一輛是12噸。承包商使用卡車從遠程地點向機場地點運送填充物。 8噸和12噸卡車的單程運營成本分別爲14.57美元和16.26美元。一輛卡車的行程不能超過60%。
編寫一個程序,爲給定的噸數開發最小成本。提示用戶輸入總噸數。顯示每輛卡車所需的行程數量和總成本。爲此程序使用模塊化設計。
這裏是我的嘗試:
#include <stdio.h>
int main (void)
{
double mass, sixtyPercentMass;
int eightCount = 0, twelveCount = 0;
printf("Enter the number of tons: ");
scanf("%lf", &mass);
sixtyPercentMass = 0.6 * mass;
while((twelveCount*12) < sixtyPercentMass) {
twelveCount++;
}
while((eightCount*8) < (mass - sixtyPercentMass)) {
eightCount++;
}
printf("Total trips required: %d\n", (twelveCount+eightCount));
printf("Number of 12-ton trucks required: %d\n", twelveCount);
printf("Number of 8-ton trucks required: %d\n", eightCount);
printf("Total cost: $%lf", (twelveCount*16.26 + eightCount*14.57));
return 0;
}
問題:
我怎麼會佔卡車浪費空的空間?
目前,如果您剩餘1個,則會浪費整個卡車。實際上,使用大量符合所需質量的卡車實際上會更便宜。我該如何正確地完成正確的總行程的60%閾值?
它目前只有質量的60%。
所需邏輯的僞代碼或正確方向的步驟將會有所幫助。
你對SO觀衆的問題是什麼? – 2013-03-12 21:19:00
關閉這個問題太局部化了:-) – 2013-03-12 21:24:27
這是什麼意思,Aniket? – user1251385 2013-03-12 21:25:47