C 编程 : Saving value in for loop and finding minimum

标签 c

过去 5 个小时我一直坐在这里试图解决这个问题。我是 C 编程的新手。

我需要能够比较 for 循环中的值并存储它们,最后打印出哪个商店的价格最便宜以及相关的商店 #。这是我所拥有的,它一直给我奇怪的输出并且没有正确地添加订单:

#include <stdio.h>
int main() {

    int shops = 0, ingredients = 0, i=0, j=0, cstore=0;
    double temp = 0.0, ingredientPrice = 0.0, total, cprice=0.0;

    printf("How many shops will you be visiting?\n");
    scanf("%d",&shops);

    for(i = 0; i < shops; i++){
        printf("You are at shop # %d\n",i+1);
        printf("How many ingredients do you need at shop #%d?\n", i+1);
        scanf("%d",&ingredients);

        for(j = 0; j < ingredients; j++) {
            printf("How much is ingredient #%d?\n", j+1);
            scanf("%lf", &temp);
            ingredientPrice += temp;
            total = ingredientPrice;

            if (shops == 1) {
                cstore = shops;
                cprice = total;
            }
            else if(total < ingredientPrice) {
                cstore = i+1;
                cprice = total;
            }
        }
        printf("Your total at shop #%d is $%lf\n", i+1, total);
    }
    printf("Your cheapest order was at shop #%d and cost $%lf.\n", cstore, cprice);
    return 0;
}

作为旁注,不能使用数组,因为我们还没有讨论那一章。任何帮助将不胜感激!请和谢谢

如果有人需要弄清楚我在问什么,这里还有作业:

您的导师指派您去几家不同的商店下订单。该程序应询问将访问的商店总数。在每个商店,询问需要购买的食材数量。对于每种成分,询问价格。跟踪订单总额,以便您可以在离开商店之前记下来。这个程序 还应该跟踪订单是最便宜的,以及最便宜的订单在哪家商店。这将帮助您的导师决定继续与谁做生意!

输入规范 1.店铺数量为正整数。 2. 配料数量为正整数。 3.每种食材的成本都是正实数。

输出规范 输出每个店铺的成本,保留两位小数。

您在#Y 的总金额为 $X.XX。

程序结束时,打印最便宜的订单。

您最便宜的订单是在 #Y 商店订购的,价格为 $X.XX。

输出样本

您将参观多少家商店? 3个 你在 1 号店。 1 号店需要多少原料? 2个 成分#1 多少钱? 3.5 配料#2 多少钱? 10 您在 1 号店的总价是 13.50 美元。 你在 2 号店。 2 号店需要多少原料? 3个 成分#1 多少钱? 2.75 配料#2 多少钱? 3个 #3 的成分是多少? 1.25 您在 2 号店的总价是 7.00 美元。 你在 3 号店。 3 号店需要多少原料? 2个 成分#1 多少钱? 4个 配料#2 多少钱? 5个 您在 3 号商店的总价是 9.00 美元。 您最便宜的订单是在 2 号商店订购的,价格为 7.00 美元。

最佳答案

阅读这个可能的解决方案。我没有费心去检查 scanf() 的返回值,但后来我很懒惰,无论如何都不将它用于生产代码。

#include <stdio.h>

int main() {

    int    i = 0, shops = 0;
    int    cstore = 0;          // current minimum store
    double cprice = 0.0;        // total for the current minimum store

    printf("How many shops will you be visiting?\n");
    scanf("%d", &shops);

    for (i = 1; i <= shops; i++){
        int    j = 0, ingredients = 0;
        double total = 0.0;      // total for the current store.

        printf("You are at shop # %d\n", i);
        printf("How many ingredients do you need at shop #%d?\n", i);
        scanf("%d", &ingredients);

        // get the prices of the ingredients at this store and total them up.
        for (j = 1; j <= ingredients; j++) {
            double ingredientPrice = 0.0;

            printf("How much is ingredient #%d?\n", j);
            scanf("%lf", &ingredientPrice);
            total += ingredientPrice;
        }
        if (i == 1) {
            // this is the first store so it is automatically the lowest thus far being the only one.
            cstore = i;
            cprice = total;
        }
        else if (total < cprice && ingredients > 0) {
            // the total for this store is less than the last known smallest total so lets update our variables.
            // if there are zero ingredients from this store then it does not count.
            cstore = i;
            cprice = total;
        }
        // the currency is US dollars with two decimal places.
        printf("Your total at shop #%d is $%.2lf and the lowest store is %d for $%.2lf.\n", i, total, cstore, cprice);
    }
    printf("Your cheapest order was at shop #%d and cost $%.2lf.\n", cstore, cprice);
    return 0;
}

我得到了这个输出。

How many shops will you be visiting?
3
You are at shop # 1
How many ingredients do you need at shop #1?
2
How much is ingredient #1?
3
How much is ingredient #2?
4
Your total at shop #1 is $7.00 and the lowest store is 1 for $7.00.
You are at shop # 2
How many ingredients do you need at shop #2?
3
How much is ingredient #1?
1
How much is ingredient #2?
3
How much is ingredient #3?
4
Your total at shop #2 is $8.00 and the lowest store is 1 for $7.00.
You are at shop # 3
How many ingredients do you need at shop #3?
3
How much is ingredient #1?
1
How much is ingredient #2?
2
How much is ingredient #3?
2
Your total at shop #3 is $5.00 and the lowest store is 3 for $5.00.
Your cheapest order was at shop #3 and cost $5.00.

关于C 编程 : Saving value in for loop and finding minimum,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42183063/

相关文章:

c - ARM-linux4.3.2,无法打开PF_PACKET类型的socket

c - KnR 练习 2-8 : Rotate a number to the right. 可以吗?

c - 从 Linux shell 向 C 代码提供输入参数

c - 具有指向指针和 strcpy 的指针的函数

c - 尽管定义为 1,变量值仍然过长

c - 为什么 sem_timedwait() 没有醒来?

c - 如何自动运行 ulimit -c unlimited

C 结构数组元素初始化转换

c - 反向双链表

c - Codeblocks 上的 Openmp