Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 程序需要优化以实现最低成本_C - Fatal编程技术网

C 程序需要优化以实现最低成本

C 程序需要优化以实现最低成本,c,C,问题出在这里: #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((twe

问题出在这里:

#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;
}
成本分析是工程设计的重要组成部分。在实践中,可能会要求您编写程序,以确定许多不同潜在情况下的最低成本。您的程序可以用作项目的决策工具

考虑修建一座机场,跑道建在垃圾填埋场上。承包商有两辆自卸卡车,一辆容量为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;
}
#包括
内部主(空)
{
双倍质量,六倍质量;
int eightCount=0,twelveCount=0;
printf(“输入吨数:”);
扫描频率(“%lf”和质量);
六类RCENTMASS=0.6*质量;
而((十二计数*12)<6TyperCentMass){
十二计数++;
}
而((八计*8)<(质量-六类RCENTMASS)){
八计数++;
}
printf(“所需总行程:%d\n”,(十二次计数+八次计数));
printf(“所需的12吨卡车数量:%d\n”,十二辆);
printf(“所需的8吨卡车数量:%d\n”,八辆);
printf(“总成本:$%lf”,(十二位数*16.26+八位数*14.57));
返回0;
}
问题:

#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;
}
  • 我如何解释在卡车上浪费空间的原因?
    目前,如果你有一个剩余,它浪费了整个卡车。实际上,使用与所需质量完全匹配的多辆卡车会更便宜

  • 我如何正确设置总行程的60%阈值?
    目前,它只能完成60%的质量


  • 伪代码或针对所需逻辑的正确方向的步骤将很有帮助。

    首先,您需要将问题从程序中分离出来。尝试在一个步骤中解决这两个问题会让您发现问题所需的内容,因为您正在处理使问题作为软件运行所需的内容

    为了优化卡车的最小数量,您应该想出一种方法来确保不使用不需要的卡车。一种方法是尝试使用最大数量的最小卡车,并通过用较大的卡车替换较小的卡车来“减少”解决方案。另一种方法是使用可用的最大卡车,然后检查部分满载的卡车,可以用较小的卡车代替

    无论你如何处理这个问题,都不需要计算机来解决。一旦你能在某种程度上解决问题而不需要计算机(即使方案只在你的头脑中),那么你就可以着手编写一个程序来完成实现你的解决方案所必需的任务


    从写作开始,作为第一步,非常像是通过开始切割和锤击木头来建造一座房子,然后想知道在没有外门、厨房、墙壁上的电线等的情况下,你将如何吃饭。

    你对这么多观众的问题是什么?以过于本地化的方式结束这个问题:-)这意味着什么,Aniket?好吧,我决定解决问题2,因为这是两个问题中更大的问题。问题是我不知道不使用电脑怎么解决这个问题。老实说,这对我来说似乎是个悖论。例如:假设我想移动120吨的质量。但是我怎么知道总的旅行次数呢?我想我可以把120除以12得到10次旅行。其中60%是6次旅行。(继续我的下一个评论)但因为我需要10次12吨卡车,我超过了60%的阈值,因此,我需要使用8吨卡车。一旦我开始使用8吨卡车,最初的总出行次数就会增加,因此,我知道需要重新计算60%的阈值,这意味着我可能最终能够使用10辆12吨卡车!百分比是某个东西的百分比,既然你还没有说出来,我假设你必须使用不超过60%的12吨卡车。在这种情况下,你要么从8吨卡车开始,移除一些,看看是否剩下60%以上的12吨卡车,要么从另一端开始工作,将8吨卡车合并为12吨卡车,然后检查适当的答案,或者你只是用一种叫做代数的奇怪的东西来解决这个问题,而不需要一个循环来检查和修改可能的答案。