- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我刚刚开始编程,所以我知道这可能是一个非常基本的错误,但我一直在尝试找出如何修复哈佛 CS50 类(class)的greedy.c 作业代码中的逻辑错误,但没有成功。我已经查找了该问题的解决方案,但它们似乎都以与我尝试的方式不同的方式解决它。我已经对其他示例进行了逆向工程,现在我理解了它们,但我真的很想知道如何使我自己的版本运行良好。
我试图通过一系列 while 循环来解决这个问题,每个循环从总欠款中减去一定的硬币值,然后将一个硬币添加到总硬币数中。对我来说,这在逻辑上似乎是有道理的,但是当我运行该程序时,它没有给我预期的输出。它只是不执行底部的 printf 语句。我希望你们中的一位能帮我解决这个问题!感谢您的帮助!
这是我的代码:
#include <stdio.h>
#include <cs50.h>
int main (void)
{
printf("How much change is needed?\n");
float owed = get_float();
int coins = 0;
/*While loops subtracting one coin from change owed, and adding one to coin count*/
while (owed >= 0.25)
{
owed = owed - 0.25;
coins = coins + 1;
}
while (owed >= 0.1)
{
owed = owed - 0.1;
coins = coins + 1;
}
while (owed >= 0.05)
{
owed = owed - 0.05;
coins = coins + 1;
}
while (owed >= 0.01)
{
owed = owed - 0.01;
coins = coins + 1;
}
/*While loops done, now print value of "coins" to screen*/
if (owed == 0)
{
printf("You need %i coins\n", coins);
}
}
编辑:
所以我又尝试了一下,并完成了“if”语句。它为我返回错误,那么程序结束时“owed”的值怎么不等于0?
#include <stdio.h>
#include <cs50.h>
int main (void)
{
printf("How much change is needed?\n");
float owed = get_float(); //Gets amount owed from user in "x.xx" format
int coins = 0; //Sets initial value of the coins paid to 0
//While loops subtracting one coin from change owed, and adding one to coin count
while (owed > 0.25)
{
owed = owed - 0.25;
coins = coins + 1;
}
while (owed > 0.1)
{
owed = owed - 0.1;
coins = coins + 1;
}
while (owed > 0.05)
{
owed = owed - 0.05;
coins = coins + 1;
}
while (owed > 0.01)
{
owed = owed - 0.01;
coins = coins + 1;
}
//While loops done, now print value of "coins" to screen
if (owed == 0)
{
printf("You need %i coins\n", coins);
}
else
{
printf("Error\n");
}
}
编辑:
因此,一旦我的代码开始工作,我就开始摆弄它并进行过度设计。这是最终(目前)版本!
#include <stdio.h>
#include <cs50.h>
#include <math.h>
#include <time.h>
int main (void)
{
srand(time(0)); //generates random seed
float price = round(rand()%500); //generates random price between 0 and 500 cents
printf("You owe %f. How much are you paying?\n", price/100); //shows user their price between 0 and 5 dollars
printf("Dollars: ");
float paymnt = get_float()*100; //gets the amount user will pay in dollars then converts to cents
int owed = round (paymnt - price); //calculates the change owed by paymnt-price
int coins = 0; //Sets initial value of the coins paid to 0
int quarters= 0;
int dimes = 0;
int nickels = 0;
int pennies = 0;
if (owed ==0 && price >0) //If someone pays in exact
{
printf("You paid the exact amount!\n");
}
else if (owed < 0) //If someone doesn't pay enough
{
printf("You didn't give us enough money!\n");
}
else //Else(We owe them change)
{
printf("Your change is %i cents\n", owed);
//While loops subtracting one coin from change owed, and adding one to coin count
while (owed >= 25)
{
owed = owed - 25;
quarters = quarters + 1;
}
while (owed >= 10)
{
owed = owed - 10;
dimes = dimes + 1;
}
while (owed >= 5)
{
owed = owed - 5;
nickels = nickels + 1;
}
while (owed >= 1)
{
owed = owed - 1;
pennies = pennies + 1;
}
//While loops done, now print each coin and total coins needed to screen
if (owed == 0)
{
coins = quarters + dimes + nickels + pennies;
printf("You need %i coins (%i quarters, %i dimes, %i nickels, and %i pennies)\n", coins, quarters, dimes, nickels, pennies);
}
else
{
printf("Error\n");
}
}
}
最佳答案
您无法真正将 float 与整数(0)进行比较,因为某些深层汇编机制
你可以做什么:
只需执行 printf("You need %i coin\n", coin)
无条件
做
if (owed <= 0.001 && owed >= -0.001)
{
printf("You need %i coins\n", coins);
}
这实际上是一种很常见的做法
关于c - 新程序员,我需要哈佛 CS50 提供的有关greedy.c的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44851727/
对于贪婪方法无法给出最优值的任何给定问题,我们可以找到一个反例来反驳该方法。 但是,是否有可能证明对于给定的问题,一般情况下任何贪心方法都不起作用。 最佳答案 我能想到的最普遍的答案是任何贪心算法都会
我想用具有不同搜索算法的 python 实现传教士和食人者。其中一种算法是 Greedy 或 A*,需要启发式函数才能工作。 我想不出任何正确的启发式方法。你能建议一个启发式吗? 最佳答案 传教士和食
因此,即使更好的解决方案即将出现,急切搜索也是您采用初始解决方案的地方...... 急切搜索的反义词是什么?我所有的谷歌搜索结果都让我引用了 Paul Revere 的骑行。在这些困惑和不确定的时代,
我被一种模式难倒了,它不断返回第一个之后的所有匹配项(它是“贪婪的”)我的模式:ISS/(?\w\S*)文本有两个匹配项: ISS/2018-03-02 国际空间站/2005-03-09 我只希望 C
我想要一个差异输出 - old line 1 + new line 1 - old line 2 + new line 2 代替 - old line 1 - old line 2 + new lin
我正在阅读明确的 ANTLR4 引用资料,并对其中一个示例(第 76 页)有疑问: STRING: '"' (ESC|.)*? '"'; fragment ESC: '\\"' | '\\\\' ;
我正在尝试使用简单的正则表达式来匹配模式,但得到了一些意想不到的结果...... 搜索模式和结果如下所示, public class Test { public static void mai
我正在尝试为表示文件路径的字符串编写解析器,可选地后跟冒号(:)和表示访问标志的字符串(例如r+ 或 w)。文件名本身可以包含冒号,例如 foo:bar.txt,因此分隔访问标志的冒号应该是字符串中的
我有以下适用于 Jupyter iPython 笔记本的“神奇”命令: %config IPCompleter.greedy=True 但是我不知道可以将其放入配置文件中,以便默认在每个新打开的笔记本
假设这是我们的文本: text = 'After 1992 , the winter and summer Olympics will be held two years apart , with t
我需要一个简单的换行算法,所以我求助于维基百科: http://en.wikipedia.org/wiki/Line_wrap_and_word_wrap#Minimum_number_of_line
问题是让 n 美分随 25 美分、10 美分、5 美分和 1 美分变化,并使用最少的硬币总数。在四种面额分别为 25 美分、10 美分、5 美分和 1 美分的特殊情况下,我们有 c1 = 25、c2
我正在阅读 tutorial关于“贪婪”算法,但我很难发现它们解决了真正的“顶级编码器”问题。 如果我知道给定的问题可以用“贪心”算法解决,那么编写解决方案的代码就很容易了。然而,如果我没有被告知这个
考虑以下字符串: 1: cccbbb 2: cccaaabbb 我想结束这样的比赛: 1: Array ( [1] => [2] => bbb ) 2: Array ( [1]
我在表格中有一行,其中有 3 个单元格,其值为 A、B 和 C,表格是屏幕的整个宽度,这对于所有 3 个来说绰绰有余。现在,当他们显示,所有单元格都占了房间的 1/3,但这不是我想要的。我希望第二个和
).)* 匹配我所有的表格标签。然而, ))* 才不是。如果我尝试用文字写出表达式,第二个似乎是有道理的,但我无法理解第一个。 有什么不同 ? 作为引用,我从这里得到了“Tempered Greedy
这两个术语是什么?可以理解吗? 最佳答案 贪婪会尽可能多地消耗。来自 http://www.regular-expressions.info/repeat.html我们看到尝试将 HTML 标签与
这两个术语是什么?可以理解吗? 最佳答案 贪婪会尽可能多地消耗。来自 http://www.regular-expressions.info/repeat.html我们看到尝试将 HTML 标签与
我正在使用各种算法实现强盗问题。我面临的问题是 epsilon-greedy 在 5 个臂和 2000 年的 epsilon 值为 0.95 时比 UCB 表现更好。我知道本地平线与许多武器相当时,e
我目前正在尝试制作一个匹配 URL 参数并提取它们的正则表达式。 例如,如果我得到以下参数字符串?param1=someValue¶m2=someOtherValue,std::regex_m
我是一名优秀的程序员,十分优秀!