C编程:在for循环中保存值并找到最小

时间:2017-02-12 00:54:33

标签: 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多少钱? 五 你在#3店的总价是9.00美元。 您最便宜的订单是在2号店,售价7.00美元。

2 个答案:

答案 0 :(得分:0)

阅读这个可能的解决方案。我没有费心去检查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.

答案 1 :(得分:-1)

只需将cprice初始化为一个相当大的值,然后在每个订单后将该变量与总数进行比较,如果变量更大则更新它。

一个简单而安全的技巧,因为ammounts必须是肯定的,才能使cprice = -1能够检查它是否已初始化。

关于您的其他问题,您的代码似乎正在调用未定义的行为,这很可能是由于错误使用了scanf()函数。

这是一个如何做到这一点的例子

#include <stdio.h>

int main() {

    int shops = 0, ingredients = 0, i = 0, j = 0, cstore = 0;
    double temp = 0.0, ingredientPrice = 0.0, total = 0.0, cprice = -1.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;
            }
        }

        if ((cprice < 0.0) || (cprice > total)) {
            cprice = total;
            cstore = i;
        }
        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;
}

我并不认为你的scanf()会导致未定义的行为,因为你仔细地初始化了所有内容。但这并不意味着这是正确或正确的方法,而是应该检查scanf()的成功,以确保输入有效。