- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
过去 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/
我有一个 合作伙伴集合,我正在使用 pymongo 来检索数据 当我使用 MongoDB 查询集合时,我看到以下结果 db.partner.find({'unique_key': 'c89dbe313
嗨,我正在尝试在一个 find 命令中查找所有 js 和 css 文件。我尝试了以下所有方法但徒劳无功: find WebContent -name "*.[jc]ss?" find WebConte
我使用以下 find 命令查找并显示所有具有输入文本模式的文件。 找 。 -type f -print|xargs grep -n "模式" 我有很多项目文件夹,每个文件夹都有自己的名为“Makefi
我在Windows环境中使用Gnuwin32二进制文件。 当我想查找某种类型的文件时(例如PDF),我通常运行: find . -iname '*.pdf' -print 这在任何UNIX系统上均可完
我使用的是 Julia 编程语言,我知道你可以通过以下方式使用 find 函数: a = [ 1 2 3 4 3 5 3 6 7 8 9 3 ] find(a .== 3) 它将返回:3,5,7,12
jsperf's link 我不是 jQuery 专家(甚至不是一个好的用户),我没有研究它的整个源代码(只有一小部分不能帮助我解决这个问题)。 有人可以为我解释一下吗? 最佳答案 这个: $p.fi
我应该如何在 CentOS 7 中修复这个错误? [jalal@goku HW4]$ git clone https://github.com/pathak22/pyflow.git Cloning
是否可以更改传递给 find 中的 exec 的参数?例如,我需要以不同的名称复制文件:*.txt -> *.new.txt现在我正在为两个命令执行此操作: find /root/test -name
我想通过cleartool find 命令找到*.cs 和*.cpp 文件。但它失败了。 cleartool find "M:\test_view\code" -name "*.cs *.cpp"
我正在使用 PyMongo,看到有人建议使用 find()[:] 而不是 find()。很好奇有什么区别? 最佳答案 [:] 制作列表的浅拷贝,因此对对象的引用是相同的。我查看了 Pymongo 文档
我正在处理文件和目录,以在每个目录中查找最近修改的文件。我的代码可以工作,但作为 Ruby 的新手,我无法正确处理错误。 我使用 Find.find 获取递归目录列表,为每个目录调用我自己的函数 ne
/usr/bin/ld: cannot find -ldlib /usr/bin/ld: cannot find -lcblas /usr/bin/ld: cannot find -llapack 在
我有一些数据文件的一系列索引文件,它们基本上采用这种格式 索引文件:asdfg.log.1234.2345.index 数据文件:asdfg.log 这个想法是搜索所有索引文件。如果值 XXXX 出现
我有一个 find我运行以查找名称包含 foo 的文件的命令. 我想跳过 .git目录。下面的命令有效 除了 它打印一个 烦人 .git任何时候它跳过 .git目录: find . ( -name .
我有以下想做的事情: find . -maxdepth 6 \( -name \*.tar.gz -o -name bediskmodel -o -name src -o -name ciao -o
当我在表中查找隐藏字段时,我看到了两个隐藏字段。但是,我想通过 ID 进一步细化这两个字段。我注意到,当我使用“包含”在整个表上使用 find 时,我得到了 2 个字段。但是,如果我对隐藏字段的查找结
我正在使用下面的命令生成文件列表及其 m5sum。问题是某些文件或文件夹的名称中有空格。我将如何处理这些? find -type f -name \* | xargs md5sum 最佳答案 尝试:
我正在使用下面的命令生成文件列表及其 m5sum。问题是某些文件或文件夹的名称中有空格。我将如何处理这些? find -type f -name \* | xargs md5sum 最佳答案 尝试:
我有一个使用正则表达式查找文件的脚本。代码如下: find $dir | grep "$regex" 脚本运行有点慢,我想优化一下。搜索需要一些时间来执行,我想从中获得更好的性能。我试过这种尝试: f
这令人沮丧。我认为问题出在 api 响应返回的对象上。也许它是在字符串中,所以我所做的就是复制“postman”的响应并将其直接粘贴到js上。这样我就可以确定它在对象/数组中。但结果还是同样的错误。
我是一名优秀的程序员,十分优秀!