【问题标题】:Simple 'Make chocolate' logic and solution review简单的“制作巧克力”逻辑和解决方案回顾
【发布时间】:2016-11-09 20:17:20
【问题描述】:

我正在研究一个编码蝙蝠问题http://codingbat.com/prob/p191363,这里已经有很多解决方案CondingBat Python puzzle results in "Timed out"

我的解决方案被标记为部分正确。试图理解问题。

我们要制作一包目标公斤数的巧克力。我们有小酒吧 (每个 1 公斤)和大条(每个 5 公斤)。返回小数 使用酒吧,假设我们总是在小酒吧之前使用大酒吧。返回 -1 如果无法完成。

makeChocolate(4, 1, 9) → 4 makeChocolate(4, 1, 10) → -1
makeChocolate(4, 1, 7) → 2

据我了解,制作巧克力的方法应该使用所有可用的大棒来达到目标​​,然后使用小棒。如果大条已经足够,则返回 0。 如果我对问题的理解有误,可以告诉我吗?

这是我的解决方案

public int makeChocolate(int small, int big, int goal) {
    if (small + big*5 < goal)  return -1;

    int smallNeeded = goal - big * 5;

    if(smallNeeded <= 0) return 0;

    return smallNeeded;
}

【问题讨论】:

  • 您在代码 sn-p 中的逻辑似乎与我非常接近。你有什么问题?
  • 代码挑战没有将我的答案标记为正确。所以我认为我对问题的理解是错误的。甚至stackoverflow.com/questions/15617869/… 有不同的解决方案@j.seashell

标签: java loops


【解决方案1】:

我的理解是这样的:

如果还不够大和小合并返回-1:

if((small + big * 5) < goal)  
{
    return -1;
}

如果足够大,则将goal 减少许多值 5(对于大柱),然后返回剩余的小柱数量:

else if((big * 5) > goal)
{
    return goal % 5;
}

如果没有足够的大条来弥补goal,则将值减去大条占用的所有公斤数,并将剩余的小数减少:

else
{
    return goal - big * 5; 
}

【讨论】:

  • 我认为如果你没有足够的小但足够大,这会失败。例如。目标是 12 和 3 大 1 小:小+大*5 = 16,即 > 目标。然而,big*5 = 15 > 12 => 返回 12%5,即 2...但我们只有一颗红宝石!我们需要 if((small + big * 5)
【解决方案2】:

蟒蛇: 尝试循环,没有通过其他测试。

def make_chocolate(small, big, goal):
        if goal <= small:
          res = small
        elif big > 0:
            for i in range(big, 0, -1):
                if goal - (5*i) <= small and goal - (5*i) >=0:
                    res = goal - (5*i)
                    break
                else:              
                  res = -1
        else:
            res = -1
   
     return res

【讨论】:

    【解决方案3】:

    这是我的详细解决方案。 我从每个测试用例开始(巧克力足够,巧克力太多,小巧克力不够,大巧克力不够),然后为每个测试用例更改数字以处理所有可能的情况。

    public int makeChocolate(int small, int big, int goal) {
    int result = goal % 5;  
    
    // means we need more little pieces
    if(result > 0){
        if((small + big*5) >= goal){
            if(goal-big*5 < 0){
                if((goal % 5) > small){
                    return -1;
                }
                else{
                    return goal % 5;
                }
            }else{
                return goal-big*5;
            }
    
        }else{
            return -1;
        }
    } 
    else{
        if(big*5 + small < goal){
            return -1;
        }
        if((goal - big*5) <= 0){
            return 0;
        }else{
            return goal - big*5;
        }   
    }
    }
    

    【讨论】:

      【解决方案4】:

      与 makeBrics 不同的是,它有点难。希望这会有所帮助:

      public int makeChocolate(int small, int big, int goal) {
      
      int bigCapacity = goal/5;
      
      if (bigCapacity>=big && goal-big*5<=small) 
       return goal-big*5;  
      else if (bigCapacity<big && goal%5<=small)
       return goal%5;
      
      else return -1;
      
      }      
      

      【讨论】:

        【解决方案5】:
        int needed_small = goal - Math.min((int) Math.floor(goal / 5), big) * 5;
        return needed_small <= small ? needed_small : -1;
        

        【讨论】:

          【解决方案6】:
          def make_chocolate(small, big, goal):
            if (goal>(small+big*5))or((goal%5)>small):
              return -1
            elif big*5>goal:
              return goal%5 
            else:
              return goal-big*5
          

          【讨论】:

            【解决方案7】:

            这是我的方法:

            public int makeChocolate(int small, int big, int goal) {
            
              if (goal % 5 <= small && goal / 5 <= big){
               return goal % 5;
              }
              else if (goal / 5 > big && small >= (goal - big * 5)){
               return goal- big * 5;
              }
              else {
               return -1;
              }
            
            }
            

            【讨论】:

              【解决方案8】:

              这是我的解决方案:

              def make_chocolate(small, big, goal):
                big_needed = goal // 5
                small_needed = goal % 5
              
                if ((small + (big * 5)) < goal) or ((big > big_needed) and (small_needed > small)):
                  return -1
                elif (big < big_needed) and (small >= (small_needed + ((big_needed-big)*5))):
                  return small_needed + ((big_needed-big)*5)
                else:
                  return small_needed
              

              【讨论】:

                【解决方案9】:
                public int makeChocolate(int small, int big, int goal) {
                  if(big*5+small<goal){
                    return -1;
                  }
                  int hmbigMax = goal/5;
                  if(hmbigMax>=big){
                    if(small>=(goal-big*5)){
                      return goal-big*5;
                    }
                    else{
                      return -1;
                    }
                  }
                  else {
                    if(small>=(goal-hmbigMax*5)){
                      return goal-hmbigMax*5;
                    }
                    else{
                      return -1;
                    }
                  }
                }
                

                这是真的,我为这个解决方案赢得了一颗星。

                【讨论】:

                  【解决方案10】:

                  def make_chocolate(small, big, goal):

                    def b_req(big, goal):
                      req = goal//5
                      if req <= big and req > 0:
                        return req
                      else:
                        return big
                  
                  
                    if goal >= 5:
                     s_req = goal - (b_req(big,goal)*5)
                    else:
                     s_req = goal
                  
                    if s_req <= small or s_req == 0:
                      return s_req
                    else:
                      return -1
                  

                  【讨论】:

                    【解决方案11】:

                    这是我的方法:

                    def make_chocolate(small, big, goal):
                    
                        while goal >= 5:
                            if big == 0:
                                break
                            goal = goal - 1
                            big = big - 1
                    
                        if goal > small:
                            return(-1)
                        else:
                            return(goal)
                    

                    【讨论】:

                    • 虽然此代码可能会为 OP 的问题提供解决方案,但强烈建议您提供有关此代码为何和/或如何回答问题的额外上下文。从长远来看,只有代码的答案通常会变得毫无用处,因为未来遇到类似问题的观众无法理解解决方案背后的原因。
                    【解决方案12】:

                    这对我来说没有循环,得到了一颗星。

                    第一个if语句用于检查是否可以完成(makeBricks的解决方案) 一旦被检查,它只会检查是否只能用大个子来完成,如果是,则返回 0, 然后它检查你是否可以使用所有的大柱,如果可以,使用它们并给出仍然需要的小柱的数量 然后下一部分取目标值,将其除以 5 以查看可以取出多少个大柱,因为我们现在知道有足够的数据,然后将其乘以 5 以实际得到从目标中减去的正确数量。

                    public int makeChocolate(int small, int big, int goal) {
                      if ((goal%5)<=small && (goal-(big*5))<=small) {
                        if (goal%5==0 && goal/5<=big)
                          return 0;
                        if (goal-big*5>0)
                          return (goal-big*5);
                        return (goal-((goal/5)*5));
                      }
                      return -1;
                    }
                    

                    【讨论】:

                      【解决方案13】:
                      def make_chocolate(small, big, goal):
                        if goal<=big*5+small:
                          if goal%5>small:
                            return -1
                          elif big*5>goal:
                            return (goal%5)
                          else:
                            return goal-(big*5)
                        else:
                          return -1
                      

                      这是我在 python 中的解决方案。如有任何错误,我深表歉意,这是我在 stackoverflow 上的第一篇文章。

                      【讨论】:

                        【解决方案14】:
                        public int makeChocolate(int small, int big, int goal) {
                          while( big >0 && (big*5) > goal )
                            big--;
                          int remain = goal - (big *5);
                          if(small - remain >= 0)
                            return remain ;
                         
                          return -1;
                        }
                        

                        【讨论】:

                        • 这比所选答案好多少?
                        【解决方案15】:
                        def make_chocolate(small, big, goal):
                            if small + big*5<goal:
                                return -1
                            elif goal%5<=small and big*5<=goal:
                                return goal - (big*5)
                            elif goal%5<=small and big*5>goal:
                                return (goal - abs(goal/5)*5)
                            else:
                                return -1
                        

                        这是一个简单的例子,展示了这段代码是如何工作的;

                        def make_chocolate(4,4,18)
                            if 4 + 4*5 < 18:
                                    return -1  (which is not the case)
                        
                            elif 18%5<=4 and 4*5<=18:
                                    return 18 - (4*5)  (this is also not the case)
                        
                            elif 18%5<=4 and 4*5>18:
                                    return (18 - abs(18/5)*5)
                        
                            else:
                                    return -1
                        

                        【讨论】:

                        • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助,质量更高,更有可能吸引投票。
                        【解决方案16】:

                        以下是我在工作状态下的python回答

                        def make_chocolate(small, big, goal):
                            rem = goal // 5
                            if big >= rem:  #if big count is greater than reminder
                               req_small = goal - 5 * rem
                               res = small - req_small  #gives you remaining small
                               if res < 0:  #this is for negative
                                  return res
                               if res >= 0:
                                  used = small - res  #subtracting  require small from small count
                                  return used
                        
                            elif big < rem:  # if big count is less than reminder
                                 req_small = goal - 5 * big
                                 res = small - req_small  #gives you how many small count remain
                                 if res < 0:  #this is for negative 
                                    return res
                                 if res >= 0:
                                    used = small - res  #subtracting  req small from small count
                                    return used
                        

                        【讨论】:

                          【解决方案17】:

                          这是我的。它结合了 makeBricks 的解决方案,但需要前面的 if 声明来捕捉需要大量小物来覆盖大物无法覆盖的实例。

                          public int makeChocolate(int small, int big, int goal) {
                              if (big * 5 <= goal && goal - big * 5 <= small) {
                                  return goal - (big * 5);
                              } else if (goal <= small + big * 5 && goal % 5 <= small) {
                                  return goal % 5;
                              }
                              return - 1;
                          }
                          

                          【讨论】:

                            【解决方案18】:

                            我的 Python 解决方案。在你的脑海中简化它时让它变得简单。花了5分钟解决。

                            def make_chocolate(small, big, goal):
                              if(small + (big*5) < goal) or (goal % 5 > small):
                                return -1
                                
                              if(big*5 < goal):
                                return (goal - (big*5))
                                
                              # If big bars are larger than the goal and have enough small bars
                              return goal % 5
                            

                            【讨论】:

                              【解决方案19】:
                              def make_chocolate(small, big, goal):
                              return [-1, goal - min(goal // 5, big) * 5][goal % 5 <= small and goal <= big * 5 + small]
                              

                              【讨论】:

                              • 欢迎来到 Stack Overflow!您介意解释为什么您的代码可以解决问题吗?这不是每个人都可以展示他们最佳解决方案的论坛。谢谢!
                              【解决方案20】:
                              def make_chocolate(small, big, goal):
                                if 5 * big + small < goal:
                                  return -1
                                if goal % 5 == 0 and goal - 5 * big <= 0:
                                  return 0
                                if goal - 5 * big >= 0:
                                  return (goal - 5 * big)
                                if goal - 5 * big < 0 and goal % 5 <= small:
                                  return goal % 5
                                else:
                                  return -1
                              

                              【讨论】:

                              • 请解释您的解决方案。没有解释且只有代码的答案会被标记为低工作量。
                              【解决方案21】:

                              没有循环的解决方案

                              def make_chocolate(small, big, goal):
                                needed = goal - big*5
                                if goal > big*5+small:
                                  return -1
                                if needed>=0:
                                  return needed
                                if needed<=0 and goal%5 <= small:
                                  return goal%5
                                else:
                                  return -1
                              

                              【讨论】:

                                【解决方案22】:

                                这是我的答案。

                                public int makeChocolate(int small, int big, int goal) {
                                    int totalBig = 5*big; // determind the total amount of big in KG.
                                
                                    if (totalBig>goal) { // if total big KG is more then what goal needs
                                        big = goal/5; // change the big to whatever it needs but not more then goal.
                                        totalBig = 5*big; // now the total big KG needed is not more then goal.
                                    }
                                
                                    int smallNeeded = goal-totalBig; // you should get the rest from here on
                                
                                    if (smallNeeded>small) {
                                        return -1; 
                                    }
                                    return smallNeeded;
                                }
                                

                                【讨论】:

                                • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助,质量更高,更有可能吸引投票。
                                【解决方案23】:

                                这是我的方法:

                                def make_chocolate(small, big, goal):
                                  nec = 5
                                  for x in range(big):
                                    if goal >= nec:
                                      goal = goal - nec
                                    else:
                                      break
                                  if goal <= small:
                                    return goal
                                  else:
                                    return -1
                                

                                【讨论】:

                                  猜你喜欢
                                  • 1970-01-01
                                  • 1970-01-01
                                  • 1970-01-01
                                  • 2022-12-12
                                  • 2012-06-04
                                  • 1970-01-01
                                  • 2018-07-16
                                  • 2010-09-05
                                  • 2012-06-20
                                  相关资源
                                  最近更新 更多