简体   繁体   English

CS50贪心算法

[英]CS50 greedy algorithm

I just started C programming with cs50. 我刚开始使用cs50进行C编程。

I tried to do the problem set about the greedy algorithm but can't seem to find the bug. 我试图做关于贪婪算法的问题集,但似乎找不到错误。 My code is below. 我的代码如下。

#include <cs50.h>
#include <stdio.h>
#include <math.h>


int main (void)
{

int count = 0;

printf("how much is the change?: ");

float change = get_float();

while(change < 0)
{
   printf("change is to be more than 0");
   change = get_float();
 }

int amount = lroundf(change*100);

while(amount > 0)
{
  if ((amount-25) >= 25)
  {
      amount  =  amount - 25;
      count++;
  }

  else if ((amount-10) >= 10)
  {
      amount =  amount - 10;
      count++;
  }

  else if ((amount-5) >= 5)
  {
      amount = amount -5;
      count++;
  }

  else if((amount-1) >= 1)
  {
      amount = amount -1;
      count ++;
      break;
  }

  else
  {
   printf("you have no change \n");   
  }
  }
  printf("your number of coins is %i\n", count);

  }

When I input my change as 1, I am given back 8 coins. 当我输入零钱时,我会得到8个硬币。 Can't seem to find where the bug is. 似乎找不到错误所在。 Can anyone help me? 谁能帮我?

Firstly, you could try running your program with values for change that return simple answers, like 1 coin, using, for example, change = 0.25 . 首先,您可以尝试使用change值返回简单答案(例如1个硬币)来运行程序,例如,使用change = 0.25 If that works, then you should start trying with some few coins, repeating one type, like you did with 1.00 , or joining a few types, like 0.06 . 如果可行,那么您应该尝试使用一些硬币,像使用1.00一样重复一种类型,或者加入诸如0.06的几种类型。 And after that, try big numbers and values with higher floating inaccuracy, like 4.10 . 然后,尝试使用较大的数字和具有较高浮动误差的值,例如4.10 Following this should lead you to your answers. 接下来,您将获得答案。

If, after trying that, you still can't find the problem, then here is the answer: the problem is with the if / else if expressions. 如果在尝试之后仍然找不到问题,那么答案就在这里:问题在于if / else if表达式。 When you are trying to count the quarters, for example, the (amount-25) >= 25 doesn't work properly. 例如,当您尝试计算季度时, (amount-25) >= 25不能正常工作。 You're trying to take away a quarter while amount is bigger or equal to 25, but your code just do that until it gets to less than 50. Developing your expression may help you see it: (amount-25) >= 25 -> (amount-25) + 25 >= 25 + 25 -> amount >= 50 . 您试图在amount大于或等于25的情况下占用四分之一,但是您的代码只会这样做直到它小于50。开发表达式可能会帮助您看到它: (amount-25) >= 25 > (amount-25) + 25 >= 25 + 25 > amount >= 50

Another problem you may find is with that break statement. 您可能会发现的另一个问题是该break语句。 It might get out from the loop earlier than expected. 它可能比预期的更早退出循环。 If you try running numbers like 0.04 and 0.03 you'll see the count stuck at 1 . 如果您尝试运行0.040.03类的数字,则会看到计数停留在1 After removing the first penny, the code breaks out of the loop leaving amount still bigger than 0. break s make it harder to see when the code is getting out of the loop and that's why many programmers recommend avoiding it whenever possible. 除去第一一分钱后,代码,跳出循环留下的amount仍大于0 break那么,我们就很难看到的代码时走出循环,这就是为什么很多程序员建议总是尽可能避。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM