【问题标题】:Expression Result Unused Greedy Algorithm表达式结果未使用贪心算法
【发布时间】:2014-03-16 05:00:59
【问题描述】:

我已经运行了这个程序并且得到了未使用的错误表达式结果。我可能做错了一些简单的事情,但我花了一天的时间试图弄清楚它无济于事。非常感谢您提供的任何帮助。

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

int main()
{
    int x, y = 0;
    printf("Enter the amount of change ");
    x = GetFloat() * 100;
    while (x != 0)
    {
        if (x >= 25)
        {
            x - 25;
            y = y + 1;
        }
        if (x >= 10 && x < 25)
        {
            x - 10;
        }   y = y + 1;
        if (x >= 5 && x < 10)
        {
            x - 5;
        }   y = y + 1;
        if (x >= 1 && x < 5)
        {   x - 1;
            y= y + 1;
        }
    }
    printf("The number of coins neccessary is %d", y);
}

【问题讨论】:

    标签: c cs50


    【解决方案1】:
        if (x >= 25)
        {
            x - 25;               // This accomplishes nothing
            y = y + 1;
        }
        if (x >= 10 && x < 25)
        {
            x - 10;               // This accomplishes nothing
        }   y = y + 1;
        if (x >= 5 && x < 10)
        {
            x - 5;                // This accomplishes  nothing
        }   y = y + 1;
        if (x >= 1 && x < 5)
        {
            x - 1;                // This accomplishes nothing
            y= y + 1;
        }
    

    在每一行中,您都从x 中减去一个数字,但您对结果没有做任何事情。如果您尝试使用结果更新 x,则需要像使用 y 一样执行操作,并将 x = 放在表达式前面。

    所以如果你想让x 下降25,你应该写:

    x = x - 25; 
    

    或者,您可以编写速记:

    x -= 25;     // Note the equal sign 
    

    【讨论】:

      【解决方案2】:

      除非您将该值分配给 x,否则所有 4 个语句 x - 25、x- 10、x- 5、x - 1 将变得无用; 因为您试图从 x 中减去该值,但您没有将新值分配给 x。

      这里是你的问题的解决方案:

      #include <stdio.h>
      #include <cs50.h>
      
      int main()
      {
          int x, y = 0;
          printf("Enter the amount of change ");
          x = GetFloat() * 100;
          while (x != 0)
          {
              if (x >= 25)
              {
                  x = x - 25;   //or x-=25;
                  y = y + 1;
              }
      
      
              if (x >= 10 && x < 25)
              {
                  x = x - 10;   //or x-=10;
                  y = y + 1;
              }
              if (x >= 5 && x < 10)
              {
                  x = x - 5;    //or x-=5;
                  y = y + 1;
              }
              if (x >= 1 && x < 5)
              {
                  x = x - 1;     //or x-=1; or x--; or --x; :)
                  y = y + 1;
              }
         }
         printf("The number of coins neccessary is %d", y);
      }
      

      【讨论】:

      • @JonathonReinhart:是的,似曾相识,但可能不是抄袭。答案(你的和 Bhavesh 的)之间的时间大约是 10 分钟。那是边缘;有足够的时间来复制,但有足够的差异可能不是,IMO,复制。
      【解决方案3】:

      我仍然相信您的循环结构。有一个除法运算符可以很好地发挥作用:

      int total = 0;
      int ncoins;
      int amount = GetFloat() * 100;
      
      assert(amount >= 0);
      
      ncoins = amount / 25;
      total += ncoins;
      amount -= ncoins * 25;
      
      assert(amount < 25);
      ncoins = amount / 10;
      total += ncoins;
      amount -= ncoins * 10;
      
      assert(amount < 10);
      ncoins = amount / 5;
      total += ncoins;
      amount -= ncoins * 5;
      
      assert(amount < 5);
      total += amount;
      

      这是手写的;你也可以设计一个循环:

      int values[] = { 25, 10, 5, 1 };
      enum { N_VALUES = sizeof(values) / sizeof(values[0]) };
      
      int total = 0;
      int ncoins;
      int amount = GetFloat() * 100;
      
      assert(amount >= 0);
      
      for (int i = 0; i < N_VALUES && amount > 0; i++)
      {
          ncoins = amount / values[i];
          total += ncoins;
          amount -= ncoins * values[i];
      }
      
      assert(amount == 0);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-02
        • 2016-02-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-07
        相关资源
        最近更新 更多