C 贪婪算法中的For循环不是';t断开,就是无限地请求输入

C 贪婪算法中的For循环不是';t断开,就是无限地请求输入,c,while-loop,do-while,cs50,C,While Loop,Do While,Cs50,我正在创建一个贪婪的循环,找到最小数量的硬币用于返回CS50的pset1的值,我无法解释为什么我的while循环无限运行 我把它弄得乱七八糟,没法让它逃走 #include <stdio.h> #include <cs50.h> #include <math.h> // declare variable change_owed, num_coins, and input globally float change_owed; float input; int

我正在创建一个贪婪的循环,找到最小数量的硬币用于返回CS50的pset1的值,我无法解释为什么我的while循环无限运行

我把它弄得乱七八糟,没法让它逃走

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

// declare variable change_owed, num_coins, and input globally
float change_owed;
float input;
int num_coins;

int main(void)
{
    // makes sure the input is non-negative
    do
    {
        input = get_float("Amount paid\n");
    }
    while(change_owed <=0);
    input = round(input);

    // begin checking 
    while(input > 0)
    {
        if(input - .25 > 0) // quarters
        {
            num_coins++; // number of coins used, to be printed later, is incremented
            input = input - .25; // coin is subtracted from total
        }
        else if (input - .10 > 0) // dimes
        {
            num_coins++;
            input = input - .10;
        }   
        else if (input - .05 > 0) // nickels
        {
            num_coins++;
            input = input - .05;
        } 
        else if (input - .01 > 0) // pennies
        {
            num_coins++;
            input = input - .01;
        } 
    }
    printf("%i", num_coins);
}
#包括
#包括
#包括
//全局声明变量change\u owed、num\u coins和input
浮动汇率;
浮点输入;
国际货币;
内部主(空)
{
//确保输入为非负
做
{
输入=获取浮动(“支付金额”);
}
同时(更改0)
{
如果(输入-.25>0)//四分之一
{
num_coins++;//用于以后打印的硬币数量增加
input=input-.25;//硬币从总数中减去
}
else if(输入-.10>0)//dimes
{
num_硬币++;
输入=输入-.10;
}   
else if(输入-.05>0)//镍币
{
num_硬币++;
输入=输入-.05;
} 
否则如果(输入-.01>0)//便士
{
num_硬币++;
输入=输入-.01;
} 
}
printf(“%i”,num_硬币);
}

第一个
do/while
循环的条件是
change\u-owed问题


while(通过不使用>=改变欠John的钱)=如果你正好投入25美分,会发生什么?一旦输入值达到0.01美分,0.01-0.01将永远不会>0。浮点永远不应用于货币计算。完全有可能
input-.01
不大于零,即使
input
大于0。所有这些都是由于舍入错误。当然,你也有Sean提到的问题。实际上,我意识到他没有声明所欠更改的值。只要我为该变量输入一个值,它就工作了,因为他使用的是,
do/while循环
也有一个问题。因此,这使Bug总数达到3,并且正在计数。Bug 4:
input=round(input)
强制
输入
为整数。因此,像
1.25
这样的输入将向下舍入为
1.00
while(input > 0)
{
    if(input - .25 > 0) // quarters
    { ... }
    else if (input - .10 > 0) // dimes
    { ... }
    else if (input - .05 > 0) // nickels
    { ... }
    else if (input - .01 > 0) // pennies
    { ... }
}
int main(void) {
  //float change_owed;
  float input;
  long long num_coins = 0;
  do {
    input = get_float("Amount paid\n");
  } while (input <= 0);
  long long input_cents = llround(input * 100.0);

  // begin checking
  while (input_cents > 0) {
    if (input_cents >= 25) {
      num_coins++;
      input_cents -= 25;
    } else if (input_cents >= 10) {
      num_coins++;
      input_cents -= 10;
    } else if (input >= 5) {
      num_coins++;
      input_cents -= 5;
    } else if (input >= 1) {
      num_coins++;
      input_cents -= 1;
    }
  }
  printf("%lld\n", num_coins);
}
  if (input_cents > 0) {
    num_coins += input_cents/25;
    input_cents %= 25;
    num_coins += input_cents/10;
    input_cents %= 10;
    num_coins += input_cents/5;
    input_cents %= 5;
    num_coins += input_cents;
  }