gpt4 book ai didi

c - 我如何完成循环?

转载 作者:行者123 更新时间:2023-11-30 15:05:54 25 4
gpt4 key购买 nike

我无法弄清楚如何运行它/在哪里放置不同的方程。作业位于代码下方。

#include <stdio.h>

#define OJ 10


//main function
int main () {

int amount_taken, weight, times_taken, cost_per_oz, num_times;
float cost_of_container, leftovers, total_cost;

printf("What is the weight (in oz.) of the original container of OJ?\n");
scanf("%d", &weight);

printf("What is the cost of the original container of OJ in dollars?\n");
scanf("%f", &cost_of_container);

printf("How many times did your roommate take your juice?\n");
scanf("%d", &times_taken);
for(num_times = 0; num_times < times_taken; num_times++) {
. while ( total_cost <= OJ) {

printf("How much juice did your roommate take this time (in oz.)?\n");
scanf("%d", &amount_taken);
total_cost = cost_per_oz * amount_taken;
cost_per_oz = cost_of_container / weight;

if ( total_cost >= OJ) {
printf("Your roommate owes you $10.00.\n");
total_cost = total_cost - 10;
}//if

printf("How much juice did your roommate take this time (in oz.)?\n");
scanf("%d", &amount_taken);

}// while

} //for

leftovers = total_cost - 10;
printf("Your roommate owes you $%.2f.\n", leftovers);

return 0;
}

你和你的室友喝了很多橙汁。您注意到您的室友喝的橙汁比您多。当他们用完时,他们会拿走你的一些。

每次室友拿走你的橙汁时,你都会向他们收费。但一次向他们收取 50 或 75 美分感觉有点傻。你想出了一个绝妙的计划!您不必在每次他们拿走您的橙汁时向他们收费,而是在他们拿走值(value) 10 美元的果汁后向他们收取费用。

程序设置:

编写一个程序来模拟这个过程。要求用户输入您购买的果汁容器的尺寸(以盎司为单位)以及这些容器的价格(以美元为单位)。然后提示用户输入室友喝了多少次果汁。最后,读出室友每次拿的金额。每当果汁的总值(value)等于或超过 10 美元时,就打印出“你的室友欠你 10 美元”。输入所有数字后,如果室友欠钱,请打印出所欠金额。

示例运行

What is the weight (in oz.) of the original container of OJ?
64
What is the cost of the original container of OJ in dollars?
3.79
How many times did your roommate take your juice?
10
How much juice did your roommate take this time (in oz.)?
30
How much juice did your roommate take this time (in oz.)?
34
How much juice did your roommate take this time (in oz.)?
24
How much juice did your roommate take this time (in oz.)?
40
How much juice did your roommate take this time (in oz.)?
64
Your roommate owes you $10.00.
How much juice did your roommate take this time (in oz.)?
64
How much juice did your roommate take this time (in oz.)?
64
How much juice did your roommate take this time (in oz.)?
18
Your roommate owes you $10.00.
How much juice did your roommate take this time (in oz.)?
20
How much juice did your roommate take this time (in oz.)?
20
Your roommate owes you $2.38.

最佳答案

请尝试此代码是否可以帮助您。变化不是很大,现在似乎打印了正确的输出。

#include <stdio.h>
#include <time.h>
#include <math.h>
#define OJ 10
int main() {
float amount_taken, weight, cost_per_oz;
int times_taken, num_times;
float cost_of_container, total_cost = 0;

printf("What is the weight (in oz.) of the original container of OJ?\n");
scanf("%f", &weight);
printf("What is the cost of the original container of OJ in dollars?\n");
scanf("%f", &cost_of_container);
cost_per_oz = cost_of_container/weight;
printf("How many times did your roommate take your juice?\n");
scanf("%d", &times_taken);
for (num_times = 0; num_times < times_taken; num_times++) {
printf("How much juice did your roommate take this time (in oz.)?\n");
scanf("%f", &amount_taken);
total_cost += cost_per_oz * amount_taken;
if (total_cost >= OJ) {
printf("Your roommate owes you $10.00.\n");
total_cost = total_cost - 10;
}
}
printf("Your roommate owes you $%.2f.\n", total_cost);
return 0;
}

测试

What is the weight (in oz.) of the original container of OJ?
10
What is the cost of the original container of OJ in dollars?
10
How many times did your roommate take your juice?
3
How much juice did your roommate take this time (in oz.)?
9
How much juice did your roommate take this time (in oz.)?
2
Your roommate owes you $10.00.
How much juice did your roommate take this time (in oz.)?
2
Your roommate owes you $3.00.

关于c - 我如何完成循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39501911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com