【问题标题】:Don't understand the logic behind the problem不明白问题背后的逻辑
【发布时间】:2020-08-16 02:24:28
【问题描述】:

我试图了解我的代码存在什么问题。 这是任务和我的代码:

编写一个名为 canPack 的方法,其中包含三个 int 类型的参数 bigCount、smallCount 和 goal。

  • 参数bigCount表示大面粉袋的数量(每袋5公斤)。

  • 参数smallCount表示小面粉袋的数量(每袋1公斤)。

  • 参数goal表示组装一个包装所需的目标面粉公斤数。

因此,bigCount 和 smallCount 的公斤数之和必须至少等于目标的值。如果可以用目标公斤面粉制作包装,则该方法应返回 true。

如果总和大于目标,请确保仅将满袋用于目标金额。例如,如果goal = 9、bigCount = 2 和smallCount = 0,则该方法应返回false,因为每个大袋子是5 公斤,不能分开。但是,如果goal = 9、bigCount = 1 和smallCount = 5,则该方法应该返回true,因为1 个完整的bigCount bag 和4 个完整的smallCount bag 相等,如果还有剩余的包也可以。

如果任何参数为负,则返回false。

输入/输出示例:

  • canPack (1, 0, 4);应该返回 false,因为 bigCount 是 1(5 公斤的大袋子),目标是 4 公斤。

  • canPack (1, 0, 5);应该返回 true,因为 bigCount 是 1(5 公斤的大袋子),目标是 5 公斤。

  • canPack (0, 5, 4);应该返回 true,因为 smallCount 是 5(1 公斤的小袋子),目标是 4 公斤,我们还剩下 1 个袋子,如上所述。

  • canPack (2, 2, 11);应该返回 true,因为 bigCount 是 2(大袋每个 5 公斤)和 smallCount 是 2(小袋 1 公斤),总共 12 公斤,目标是 11 公斤。

  • canPack (-3, 2, 12);应该返回 false,因为 bigCount 是负数。

    public static boolean canPack(int bigCount, int smallCount, int goal) {

        int bigCountKilos = bigCount * 5;
        int smallCountKilos = smallCount;

        if (bigCount < 0 || smallCount < 0 || goal < 0) {
            return false;
        } else {

            if (bigCountKilos >= goal && bigCountKilos % goal == 0) {
                return true;
            } else if (bigCountKilos + smallCountKilos >= goal && bigCountKilos % goal == 0) {
                return true;
            } else if (bigCountKilos < goal && bigCountKilos + smallCountKilos >= goal) {
                return true;
            } else {
                return false;
            }
        }
    }

    public static void main(String[] args) {
        System.out.println(canPack(4, 18, 19));
    }

4,18,19 的实例应该返回 true,但事实并非如此。有人可以告诉我我做错了什么吗? 提前致谢

【问题讨论】:

  • 请解释bigCountKilos + smallCountKilos &gt;= goal &amp;&amp; bigCountKilos % goal == 0的原因
  • 另外,请问:这是某种任务吗?另外,想想goal = 20, big = 4, small = 3的案例@
  • 对不起goal = 19, big = 4, small = 3

标签: java methods boolean


【解决方案1】:

这是面粉打包机问题的解决方案

public class FlourPacker {

    public static boolean canPack(int bigCount, int smallCount, int goal) {
        boolean  bool=false;
        int bigCountKilos = bigCount * 5;
    
        if(bigCount == 0 && smallCount>=goal) {
             bool= true;
        } else if(smallCount==0 && bigCountKilos>= goal && goal%bigCountKilos==0) {
            bool= true;
        } else if (bigCount > 0 && smallCount>0 && goal >= 0) {
            int bigCountTotal= bigCount*5;
            int total=bigCountTotal+smallCount;
            if(total==goal){
                 bool= true;
            }
            
            if(goal/5<bigCount && goal%5 <=smallCount){
                bool= true;
            }
            
            if(bigCountTotal<=goal && goal-bigCountTotal <=smallCount) {
                bool= true;
            }
        }
        
        return bool;
    }
}

【讨论】:

    【解决方案2】:

    public static boolean canpack(int bigcount,int smallcount,int goal) {

        if(bigcount<0||smallcount<0||goal<0) {
            return false;
        }
        int a=bigcount*5;
        int b=smallcount*1;
        if(a+b>goal&&a<goal) {
            return true;
        }else if(a+b==goal) {
            return true;
        }else {
            return false;
        }
    }
    

    希望它有效。如果不是请告诉我。

    【讨论】:

    • 请添加更多详细信息以扩展您的答案,例如工作代码或文档引用。
    • edit您的回答提供有关您提供的代码如何工作的更多信息。纯代码答案可能会解决原始提问者的问题,但它们无法帮助未来的读者理解解决方案。
    【解决方案3】:

    此代码适用于贪心算法并通过所有测试用例,试一试:

    public static boolean canPack(int bigCount, int smallCount, int goal) {
        // Here we are checking if the inputs are valid
        if (bigCount < 0 || smallCount < 0 || goal < 0 || (bigCount*5 + smallCount)  < goal) {
            return false;
        }
    
        // Here we multiply bigCount with 5 since each bigCount contains 5 kg
        int bigCountKilo = bigCount * 5;
    
        // We check if goal > 5 and then subtract bigCount from it till the goal is less than 5kg
        while (goal >= 5) {
            if (bigCountKilo > 0) {
                goal -= 5;
                bigCount-=1;
            } else {
                break;
            }
        }
    
        // Similarly here too we are checking if goal is greater than 0 and then subtracting smallCount values
        while (goal > 0) {
            if (smallCount > 0) {
                goal -= 1;
                smallCount -= 1;
            } else {
                break;
            }
        }
    
        // We are returning true if goal is reached (if goal becomes zero)
        return (goal == 0);
    }
    

    【讨论】:

    • 您可以将code block 用于code section,这将使问题/答案更具可读性和更好。有关更多信息,请查看link
    【解决方案4】:

    嘿,解释很简单 直到bigCountsmallCountgoal为止,我希望你已经理解了上半部分。

    那么让我们开始吧... 让我们假设goal = 9kg。这意味着您必须制作一个包含 9 公斤面粉的包装。 现在bigCount= 2(表示10公斤)和smallCount = 0

    你有多少面粉(bigCount*5) + smallCount = 10kg 你有足够的面粉做一个包裹吗?是的。 你能做一个9公斤的包裹吗?不。 因为你有两袋,每袋 5 公斤,你不能从任何袋子里取出 1 公斤。

    考虑第二种情况 让我们假设goal = 9kg。这意味着,您必须制作一个包含 9 公斤面粉的包装。

    现在bigCount= 1(表示 5 公斤)和smallCount = 5(表示 5 公斤)。

    现在你还有 10 公斤面粉,但问题是,你能在不从袋子里取出面粉的情况下做一个包裹吗?这次,是的。

    5kg + 1kg + 1kg + 1kg +1kg = 9kg1kg extra

    简单地告诉你,你可以有额外的面粉,但你不能为了达到目标而从袋子里取出面粉。

    【讨论】:

      【解决方案5】:
      public static boolean canPack(int bigCount, int smallCount, int goal) {
          // Input validation.
          if (bigCount < 0 || smallCount < 0 || goal < 0) {
              return false;
          }
          // Create a variable for the the bigCount variable to show it in kg.
          int bigCountInKg = bigCount * 5;
          
          if (bigCountInKg < goal) {
              int result = goal - bigCountInKg;
              if (result <= smallCount) {
                  return true;
              } else {
                  return false;
              }
          } else if (bigCountInKg == goal) {
              return true;
          } else if (bigCountInKg > goal) {
              int maxBigCount = goal / 5;
              int result = goal - maxBigCount;
              if (result <= smallCount) {
                  return true;
              }
          }
          return false;
      }
      

      【讨论】:

      • 虽然这个代码块可能会回答这个问题,但最好能稍微解释一下为什么会这样。
      【解决方案6】:
      int bigCountKilo = bigCount*5;
          if(bigCountKilo+smallCount*1 == goal){
              return true;
          }else if((bigCountKilo>=goal&&bigCountKilo%goal == 0)||(bigCountKilo>=goal&&goal%5<=smallCount)){
              return true;
          }else if(smallCount>=goal){
              return true;
          }else if(bigCountKilo+smallCount>goal&&goal%bigCountKilo<=smallCount){
              return true;
          }else{
              return false;
          }
      }
      

      这适用于所有条件。请试试这个,如果有任何问题请更新。

      【讨论】:

        【解决方案7】:
        private static boolean canPack(
            final int bigCount,
            final int smallCount,
            final int goal
        ) {
        
            // return goal - Math.min(goal / 5, bigCount) * 5 <= smallCount;
        
            final int numberOfBigNeeded = goal / 5;
            final int numberOfBigUsed = Math.min(numberOfBigNeeded, bigCount);
            final int remainingKilosFromGoalAfterUsingBig = goal - numberOfBigUsed * 5;
            final boolean hasEnoughSmallToCoverRemainingKilosFromGoalAfterUsingBig = remainingKilosFromGoalAfterUsingBig <= smallCount;
            return hasEnoughSmallToCoverRemainingKilosFromGoalAfterUsingBig;
        }
        

        【讨论】:

        • 你需要解释你的答案,以确保提问者理解这一点。
        【解决方案8】:

        我觉得你找到答案的逻辑不正确。 if 语句并未涵盖所有情况,也并非每次都给出正确答案。

        一个基本的方法是这样的:

        • goal 减 5,当 goal > 0 且 bigCount > 0 时 bigCount 减 1
        • 如果目标仍然 > 0,请检查 smallCount >= 目标。如果是,则返回 true,否则返回 false。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-06-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多