📜  巧克力和包装拼图程序

📅  最后修改于: 2021-04-28 18:15:20             🧑  作者: Mango

给定以下三个值,任务是找到可以吃的最大巧克力总数。

  1. 钱:你必须买巧克力的钱
  2. 价格:巧克力的价格
  3. 包裹:要获得一份额外巧克力的包装返回的数量。

可以假定所有给定值都是正整数且大于1。
例子:

Input: money = 16, price = 2, wrap = 2
Output:   15
Price of a chocolate is 2. You can buy 8 chocolates from
amount 16. You can return 8 wrappers back and get 4 more
chocolates. Then you can return 4 wrappers and get 2 more
chocolates. Finally you can return 2 wrappers to get 1
more chocolate.

Input:   money = 15, price = 1, wrap = 3
Output:   22
We buy and eat 15 chocolates
We return 15 wrappers and get 5 more chocolates.
We return 3 wrappers, get 1 chocolate and eat it
(keep 2 wrappers). Now we have 3 wrappers. Return
3 and get 1 more chocolate.
So total chocolates = 15 + 5 + 1 + 1

Input:  money = 20, price = 3, wrap = 5
Output:   7

来源:拼图22 | (最大巧克力)

天真的方法是通过返回包装纸,直到剩下的包装纸不小于获取巧克力所需的数量,来连续计算巧克力的数量。

下面是此方法的实现。

C++
// Recursive C++ program to find maximum
// number of chocolates
#include 
using namespace std;
 
// Returns number of chocolates we can
// have from given number of chocolates
// and number of wrappers required to
// get a chocolate.
int countRec(int choc, int wrap)
{
    // If number of chocolates is less than
    // number of wrappers required.
    if (choc < wrap)
        return 0;
 
    // We can immediatly get newChoc using
    // wrappers of choc.
    int newChoc = choc/wrap;
 
    // Now we have "newChoc + choc%wrap" wrappers.
    return newChoc + countRec(newChoc + choc%wrap,
                              wrap);
}
 
// Returns maximum number of chocolates we can eat
// with given money, price of chocolate and number
// of wrappers required to get a chocolate.
int countMaxChoco(int money, int price, int wrap)
{
    // We can directly buy below number of chocolates
    int choc = money/price;
 
    // countRec returns number of chocolates we can
    // have from given number of chocolates
    return choc + countRec(choc, wrap);
}
 
// Driver code
int main()
{
    int money = 15 ; // total money
    int price = 1; // cost of each candy
    int wrap = 3 ; // no of wrappers needs to be
    // exchanged for one chocolate.
 
    cout << countMaxChoco(money, price, wrap);
    return 0;
}


Java
// Recursive java program to find maximum
// number of chocolates
 
import java.io.*;
 
class GFG {
 
    // Returns number of chocolates we can
    // have from given number of chocolates
    // and number of wrappers required to
    // get a chocolate.
    static int countRec(int choc, int wrap)
    {
         
        // If number of chocolates is less than
        // number of wrappers required.
        if (choc < wrap)
            return 0;
     
        // We can immediatly get newChoc using
        // wrappers of choc.
        int newChoc = choc / wrap;
     
        // Now we have "newChoc + choc%wrap"
        // wrappers.
        return newChoc + countRec(newChoc +
                          choc % wrap, wrap);
    }
     
    // Returns maximum number of chocolates
    // we can eat with given money, price of
    // chocolate and number of wrappers
    // required to get a chocolate.
    static int countMaxChoco(int money,
                          int price, int wrap)
    {
         
        // We can directly buy below number of
        // chocolates
        int choc = money/price;
     
        // countRec returns number of chocolates
        // we can have from given number of
        // chocolates
        return choc + countRec(choc, wrap);
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int money = 15 ; // total money
        int price = 1; // cost of each candy
         
        // no of wrappers needs to be
        // exchanged for one chocolate.
        int wrap = 3 ;
        System.out.println(
            countMaxChoco(money, price, wrap));
    }
}
 
// This code is contributed by anuj_67.


Python3
# Recursive Python3 program to find
# maximum number of chocolates
import math
 
# Returns number of chocolates we can
# have from given number of chocolates
# and number of wrappers required to
# get a chocolate.
def countRec(choc, wrap):
     
    # If number of chocolates is less
    # than number of wrappers required.
    if (choc < wrap):
        return 0;
 
    # We can immediatly get newChoc
    # using wrappers of choc.
    newChoc = choc / wrap;
 
    # Now we have "newChoc + choc%wrap" wrappers.
    return newChoc + countRec(newChoc + choc %
                                  wrap, wrap);
 
# Returns maximum number of chocolates
# we can eat with given money, price
# of chocolate and number of wrappers
# required to get a chocolate.
def countMaxChoco(money, price, wrap):
     
    # We can directly buy below
    # number of chocolates
    choc = money / price;
 
    # countRec returns number
    # of chocolates we can
    # have from given number
    # of chocolates
    return math.floor(choc + countRec(choc, wrap));
 
# Driver code
 
# total money
money = 15;
     
# cost of each candy
price = 1;
     
# no of wrappers needs to be
wrap = 3 ;
     
# exchanged for one chocolate.
print(countMaxChoco(money, price, wrap));
 
# This code is contributed by mits


C#
// Recursive C# program to find maximum
// number of chocolates
 
using System;
 
class GFG {
 
    // Returns number of chocolates we can
    // have from given number of chocolates
    // and number of wrappers required to
    // get a chocolate.
    static int countRec(int choc, int wrap)
    {
         
        // If number of chocolates is less than
        // number of wrappers required.
        if (choc < wrap)
            return 0;
     
        // We can immediatly get newChoc using
        // wrappers of choc.
        int newChoc = choc / wrap;
     
        // Now we have "newChoc + choc%wrap"
        // wrappers.
        return newChoc + countRec(newChoc +
                        choc % wrap, wrap);
    }
     
    // Returns maximum number of chocolates
    // we can eat with given money, price of
    // chocolate and number of wrappers
    // required to get a chocolate.
    static int countMaxChoco(int money,
                        int price, int wrap)
    {
         
        // We can directly buy below number of
        // chocolates
        int choc = money/price;
     
        // countRec returns number of chocolates
        // we can have from given number of
        // chocolates
        return choc + countRec(choc, wrap);
    }
     
    // Driver code
    public static void Main ()
    {
        int money = 15 ; // total money
        int price = 1; // cost of each candy
         
        // no of wrappers needs to be
        // exchanged for one chocolate.
        int wrap = 3 ;
        Console.WriteLine(
            countMaxChoco(money, price, wrap));
    }
}
 
// This code is contributed by anuj_67.


PHP


Javascript


C++
// Efficient C++ program to find maximum
// number of chocolates
#include 
using namespace std;
 
// Returns maximum number of chocolates we can eat
// with given money, price of chocolate and number
// of wrapprices required to get a chocolate.
int countMaxChoco(int money, int price, int wrap)
{
    // Corner case
    if (money < price)
       return 0;
 
    // First find number of chocolates that
    // can be purchased with the given amount
    int choc = money / price;
 
    // Now just add number of chocolates with the
    // chocolates gained by wrapprices
    choc = choc + (choc - 1) / (wrap - 1);
    return choc;
}
 
// Driver code
int main()
{
    int money = 15 ; // total money
    int price = 1; // cost of each candy
    int wrap = 3 ; // no of wrappers needs to be
    // exchanged for one chocolate.
 
    cout << countMaxChoco(money, price, wrap);
    return 0;
}


Java
// Efficient Java program to find maximum
// number of chocolates
import java.io.*;
 
class GFG {
 
    // Returns maximum number of chocolates
    // we can eat with given money, price
    // of chocolate and number of wrapprices
    // required to get a chocolate.
    static int countMaxChoco(int money,
                        int price, int wrap)
    {
         
        // Corner case
        if (money < price)
            return 0;
     
        // First find number of chocolates
        // that can be purchased with the
        // given amount
        int choc = money / price;
     
        // Now just add number of chocolates
        // with the chocolates gained by
        // wrapprices
        choc = choc + (choc - 1) / (wrap - 1);
        return choc;
    }
     
    // Driver code
    public static void main (String[] args)
    {
         
        // total money
        int money = 15;
         
        // cost of each candy
        int price = 1;
         
        // no of wrappers needs to be
        int wrap = 3 ;
         
        // exchanged for one chocolate.
        System.out.println(
           countMaxChoco(money, price, wrap));
    }
}
 
// This code is contributed by anuj_67.


Python3
# Efficient Python3 program to find
# maximum number of chocolates
 
# Returns maximum number of
# chocolates we can eat with
# given money, price of chocolate
# and number of wrapprices
# required to get a chocolate.
def countMaxChoco(money, price, wrap) :
 
    # Corner case
    if (money < price) :
        return 0
 
    # First find number of chocolates
    # that can be purchased with the
    # given amount
    choc = int(money / price)
 
    # Now just add number of
    # chocolates with the chocolates
    # gained by wrapprices
    choc = choc + (choc - 1) / (wrap - 1)
    return int(choc )
 
# Driver code
money = 15 # total money
price = 1 # cost of each candy
wrap = 3 # no of wrappers needs to be
         # exchanged for one chocolate.
 
print(countMaxChoco(money, price, wrap))
 
# This code is contributed by Smitha


C#
// Efficient C# program to find maximum
// number of chocolates
using System;
 
class GFG {
 
    // Returns maximum number of chocolates
    // we can eat with given money, price
    // of chocolate and number of wrapprices
    // required to get a chocolate.
    static int countMaxChoco(int money,
                        int price, int wrap)
    {
         
        // Corner case
        if (money < price)
            return 0;
     
        // First find number of chocolates
        // that can be purchased with the
        // given amount
        int choc = money / price;
     
        // Now just add number of chocolates
        // with the chocolates gained by
        // wrapprices
        choc = choc + (choc - 1) / (wrap - 1);
        return choc;
    }
     
    // Driver code
    public static void Main ()
    {
         
        // total money
        int money = 15;
         
        // cost of each candy
        int price = 1;
         
        // no of wrappers needs to be
        int wrap = 3 ;
         
        // exchanged for one chocolate.
        Console.WriteLine(
        countMaxChoco(money, price, wrap));
    }
}
 
// This code is contributed by anuj_67.


PHP


Javascript


输出 :

22

一种有效的解决方案是使用直接公式来查找巧克力的数量。

Find initial number of chocolates by
dividing the amount with per piece cost.
i.e. choc = money / price

then apply below formula
choc += (choc - 1)/(wrap - 1)

在上述幼稚的实现中,我们注意到在找到巧克力的初始数量之后,我们将巧克力的数量除以所需的包装材料的数量。直到我们剩下1个巧克力或包装纸为止。
我们正在重新计算值,即((choc / wrap + choc%wrap)/ wrap,直到得到1。
可以看到,通过将巧克力和包装纸的值减1,然后将它们除以得到结果(choc-1)/(wrap-1) ,就可以得到结果。

下面是上述方法的实现:

C++

// Efficient C++ program to find maximum
// number of chocolates
#include 
using namespace std;
 
// Returns maximum number of chocolates we can eat
// with given money, price of chocolate and number
// of wrapprices required to get a chocolate.
int countMaxChoco(int money, int price, int wrap)
{
    // Corner case
    if (money < price)
       return 0;
 
    // First find number of chocolates that
    // can be purchased with the given amount
    int choc = money / price;
 
    // Now just add number of chocolates with the
    // chocolates gained by wrapprices
    choc = choc + (choc - 1) / (wrap - 1);
    return choc;
}
 
// Driver code
int main()
{
    int money = 15 ; // total money
    int price = 1; // cost of each candy
    int wrap = 3 ; // no of wrappers needs to be
    // exchanged for one chocolate.
 
    cout << countMaxChoco(money, price, wrap);
    return 0;
}

Java

// Efficient Java program to find maximum
// number of chocolates
import java.io.*;
 
class GFG {
 
    // Returns maximum number of chocolates
    // we can eat with given money, price
    // of chocolate and number of wrapprices
    // required to get a chocolate.
    static int countMaxChoco(int money,
                        int price, int wrap)
    {
         
        // Corner case
        if (money < price)
            return 0;
     
        // First find number of chocolates
        // that can be purchased with the
        // given amount
        int choc = money / price;
     
        // Now just add number of chocolates
        // with the chocolates gained by
        // wrapprices
        choc = choc + (choc - 1) / (wrap - 1);
        return choc;
    }
     
    // Driver code
    public static void main (String[] args)
    {
         
        // total money
        int money = 15;
         
        // cost of each candy
        int price = 1;
         
        // no of wrappers needs to be
        int wrap = 3 ;
         
        // exchanged for one chocolate.
        System.out.println(
           countMaxChoco(money, price, wrap));
    }
}
 
// This code is contributed by anuj_67.

Python3

# Efficient Python3 program to find
# maximum number of chocolates
 
# Returns maximum number of
# chocolates we can eat with
# given money, price of chocolate
# and number of wrapprices
# required to get a chocolate.
def countMaxChoco(money, price, wrap) :
 
    # Corner case
    if (money < price) :
        return 0
 
    # First find number of chocolates
    # that can be purchased with the
    # given amount
    choc = int(money / price)
 
    # Now just add number of
    # chocolates with the chocolates
    # gained by wrapprices
    choc = choc + (choc - 1) / (wrap - 1)
    return int(choc )
 
# Driver code
money = 15 # total money
price = 1 # cost of each candy
wrap = 3 # no of wrappers needs to be
         # exchanged for one chocolate.
 
print(countMaxChoco(money, price, wrap))
 
# This code is contributed by Smitha

C#

// Efficient C# program to find maximum
// number of chocolates
using System;
 
class GFG {
 
    // Returns maximum number of chocolates
    // we can eat with given money, price
    // of chocolate and number of wrapprices
    // required to get a chocolate.
    static int countMaxChoco(int money,
                        int price, int wrap)
    {
         
        // Corner case
        if (money < price)
            return 0;
     
        // First find number of chocolates
        // that can be purchased with the
        // given amount
        int choc = money / price;
     
        // Now just add number of chocolates
        // with the chocolates gained by
        // wrapprices
        choc = choc + (choc - 1) / (wrap - 1);
        return choc;
    }
     
    // Driver code
    public static void Main ()
    {
         
        // total money
        int money = 15;
         
        // cost of each candy
        int price = 1;
         
        // no of wrappers needs to be
        int wrap = 3 ;
         
        // exchanged for one chocolate.
        Console.WriteLine(
        countMaxChoco(money, price, wrap));
    }
}
 
// This code is contributed by anuj_67.

的PHP


Java脚本


输出 :

22