📜  分割棒问题

📅  最后修改于: 2021-06-25 21:53:01             🧑  作者: Mango

给定一个整数列表,每个整数表示每个棍子的长度,以及一个整数,该整数告诉我们可以将一根棍子分成两半的次数,我们必须找到可以从一组棍子中获得的最大所需长度的棍子。
注意1:当我们打断一根棍子时,它会转换成两个半部分,例如,对于长度为10的一根棍子,可以得到长度为5的两个棍子,对于长度为5的一根棍子,则可以得到长度为2和3的两个棍子。分别。
注意2:丢弃的零件不能再次用于制作木棍,因此,如果给出长度为11的木棍,我们可以将其分成5和6个长度的碎片,那么我们必须丢弃其中一个无法使用的碎片更多。
例子:

方法:
为了解决上述问题,我们将首先进行线性搜索操作,以找到所有长度与所需棒长度完全相同的棒,并对它们进行计数。然后,我们将计数存储在变量中。显然,我们必须丢弃所有长度小于所需长度的棒,因为我们无法制作任何所需长度的棒。然后将长度大于所需长度的棒的值传递给一个函数,该函数计算通过折断可以获得多少根棒。在递归的帮助下,可以找到许多获取摇杆的方法。

下面是上述方法的实现:

C++
#include
using namespace std;
  
// Method to find number of sticks by breaking them 
int sticks_break(int stick_length,int n, 
                 int desired_length)
{
  
    // If stick cant be break any more 
    if (n < 1)
        return 0;
      
    // Check if stick length became 
    // smaller than the desired length 
    if (stick_length < desired_length)
        return 0;
          
    // Check if stick length is even number 
    if (stick_length % 2 == 0)
          
        // Check if half of stick 
        // length is equal to desired length 
        if (stick_length / 2 == desired_length) 
            return 2;
              
        // Check if half of stick length 
        // is greater than the desired length 
        else if (stick_length / 2 > desired_length)
            return (sticks_break(stick_length / 2,
                        n - 1, desired_length));
              
    // Check if stick length is odd number 
    if (stick_length % 2 != 0)
      
        // For odd number two halves will be 
        // generated checking if first half 
        // is equal to desired length 
        if (stick_length / 2 == desired_length) 
            return 1;
              
        // Checking if second half 
        // is equal to desired length 
        if (stick_length / 2 + 1 == desired_length)
            return 1;
          
        // Check if half of stick length 
        // is greater than the desired length 
        if (stick_length/2 > desired_length)
            return (max (sticks_break(
                         stick_length / 2, n - 1,
                         desired_length),
                         sticks_break(
                         stick_length / 2 + 1,
                         n - 1, desired_length))); 
          
    return 0;
} 
  
// Method to find number of sticks     
int numberOfSticks(vectorlist_length,
                   int n, int desired_length)
{
    int count = 0;
      
    for(auto stick_lenght : list_length)
    {
          
        // Check if desired length is found 
        if (desired_length == stick_lenght)
          
            // Incrementing the count 
            count = count + 1;
              
        // Check if stick length is 
        // greater than desired length 
        else if (stick_lenght> desired_length) 
          
            // Incrementing count 
            // after break the sticks 
            count = count + sticks_break(
                            stick_lenght, n,
                            desired_length);
    } 
      
    // Return count 
    return count;
}
      
// Driver code
int main()
{
      
    // List of integers 
    vectorlist_length = { 1, 2, 3, 21 };
      
    // Number of ways stick can be break 
    int n = 3;
      
    // Desired length
    int desired_length = 3;
      
    int count = numberOfSticks(list_length, n, 
                            desired_length);
                              
    // Print result
    cout << count << endl;
}
  
// This code is contributed by Stream_Cipher


Java
import java.util.*; 
  
class GFG{
      
// Method to find number of sticks by breaking them 
static int sticks_break(int stick_length,
                        int n, int desired_length)
{
  
    // If stick cant be break any more 
    if (n < 1)
        return 0;
      
    // Check if stick length became 
    // smaller than the desired length 
    if (stick_length < desired_length)
        return 0;
          
    // Check if stick length is even number 
    if (stick_length % 2 == 0)
          
        // Check if half of stick 
        // length is equal to desired length 
        if (stick_length / 2 == desired_length) 
            return 2;
              
        // Check if half of stick length 
        // is greater than the desired length 
        else if (stick_length / 2 > desired_length)
            return (sticks_break(stick_length / 2,
                        n - 1, desired_length));
              
    // Check if stick length is odd number 
    if (stick_length % 2 != 0)
      
        // For odd number two halves will be 
        // generated checking if first half
        // is equal to desired length 
        if (stick_length / 2 == desired_length) 
            return 1;
              
        // Checking if second half 
        // is equal to desired length 
        if (stick_length / 2 + 1 == desired_length)
            return 1;
          
        // Check if half of stick length 
        // is greater than the desired length 
        if (stick_length/2 > desired_length)
            return (Math.max (sticks_break(
                               stick_length / 2,
                               n - 1, desired_length),
                               sticks_break(
                               stick_length / 2 + 1, 
                               n - 1, desired_length))); 
          
    return 0;
} 
  
// Method to find number of sticks 
static int numberOfSticks(int list_length[],
                          int n, int desired_length)
{
    int count = 0;
      
    for(int i = 0; i < list_length.length; i++)
    {
          
        // Check if desired length is found 
        if (desired_length == list_length[i])
          
            // Incrementing the count 
            count = count + 1;
              
        // Check if stick length is 
        // greater than desired length 
        else if (list_length[i]> desired_length)
          
            // Incrementing count 
            // after break the sticks 
            count = count + sticks_break(list_length[i],
                                   n, desired_length);
    } 
      
    // Return count 
    return count;
}
  
// Driver code
public static void main(String args[]) 
{ 
      
    // List of integers 
    int[] list_length = new int[]{ 1, 2, 3, 21 }; 
      
    // Number of ways stick can be break 
    int n = 3;
      
    // Desired length
    int desired_length = 3;
      
    int count = numberOfSticks(list_length, n,
                            desired_length);
                              
    // Print result
    System.out.println(count);
}
}
  
// This code is contributed by Stream_Cipher


Python3
# method to find number of sticks by breaking them 
def sticks_break(stick_length, n, desired_length): 
      
    # if stick cant be break any more 
    if n < 1: 
          
        return 0
      
    # check if stick length became 
    # smaller than the desired length 
    if stick_length < desired_length: 
          
        return 0
          
    # check if stick length is even number 
    if stick_length % 2 == 0: 
          
        # check if half of stick 
        # length is equal to desired length 
        if stick_length / 2 == desired_length: 
              
            return 2
              
        # check if half of stick length 
        # is greater than the desired length 
        elif stick_length / 2 > desired_length: 
              
            return sticks_break(stick_length / 2, n-1, desired_length) 
              
    # check if stick length is odd number 
    if stick_length % 2 != 0: 
          
        # for odd number two halves will be generated 
        # checking if first half is equal to desired length 
        if stick_length // 2 == desired_length: 
              
            return 1
          
        # checking if second half 
        # is equal to desired length 
        if stick_length // 2 + 1 == desired_length: 
              
            return 1
          
        # check if half of stick length 
        # is greater than the desired length 
        if stick_length//2 > desired_length: 
              
            return max (sticks_break(stick_length//2, n-1, desired_length), sticks_break(stick_length//2 + 1, n-1, desired_length)) 
          
      
    return 0
  
  
# method to find number of sticks 
def numberOfSticks(list_length, n, desired_length): 
      
    count = 0
      
    for stick_length in list_length: 
          
        # check if desired length is found 
        if desired_length == stick_length: 
              
            # incrementing the count 
            count = count + 1
          
        # check if stick length is 
        # greater than desired length 
        elif stick_length > desired_length: 
              
            # incrementing count 
            # after break the sticks 
            count = count + sticks_break(stick_length, n, desired_length) 
      
    # return count     
    return count 
  
# driver code 
if __name__ == "__main__": 
      
    # list of integers 
    list_length =[1, 2, 3, 21] 
      
    # number of ways stick can be break 
    n = 3
      
    # desired length 
    desired_length = 3
  
    count = numberOfSticks(list_length, n, desired_length) 
      
    # print result 
    print(str(count))


C#
using System; 
class GFG{
      
// Method to find number of sticks by breaking them 
static int sticks_break(int stick_length,
                        int n, int desired_length)
{
  
    // If stick cant be break any more 
    if (n < 1 )
        return 0;
      
    // Check if stick length became 
    // smaller than the desired length 
    if (stick_length < desired_length)
        return 0;
          
    // Check if stick length is even number 
    if (stick_length % 2 == 0)
          
        // Check if half of stick 
        // length is equal to desired length 
        if (stick_length / 2 == desired_length) 
            return 2;
              
        // Check if half of stick length 
        // is greater than the desired length 
        else if (stick_length / 2 > desired_length)
            return (sticks_break(stick_length / 2,
                        n - 1, desired_length));
              
    // Check if stick length is odd number 
    if (stick_length % 2 != 0)
      
        // For odd number two halves will be
        // generated checking if first half 
        // is equal to desired length 
        if (stick_length / 2 == desired_length) 
            return 1;
              
        // Checking if second half 
        // is equal to desired length 
        if (stick_length / 2 + 1 == desired_length)
            return 1;
          
        // Check if half of stick length 
        // is greater than the desired length 
        if (stick_length/2 > desired_length)
            return (Math.Max(sticks_break(
                             stick_length / 2,
                             n - 1, desired_length),
                             sticks_break(
                             stick_length / 2 + 1, 
                             n - 1, desired_length))); 
          
    return 0;
} 
  
// Method to find number of sticks 
static int numberOfSticks(int []list_length,
                          int n, int desired_length)
{
    int count = 0;
    for(int i = 0; i < list_length.Length; i++)
    {
          
        // Check if desired length is found 
        if (desired_length == list_length[i])
          
            // Incrementing the count 
            count = count + 1;
              
        // Check if stick length is 
        // greater than desired length 
        else if (list_length[i]> desired_length) 
          
            // Incrementing count 
            // after break the sticks 
            count = count + sticks_break(list_length[i],
                                   n, desired_length);
    } 
      
    // Return count 
    return count;
}
  
// Driver code
public static void Main() 
{ 
      
    // list of integers 
    int []list_length = { 1, 2, 3, 21 };
      
    // Number of ways stick can be break 
    int n = 3;
      
    // Desired length
    int desired_length = 3;
    int count = numberOfSticks(list_length,
                         n, desired_length);
                           
    // Print result
    Console.WriteLine(count); 
}
}
  
// This code is contributed by Stream_Cipher


输出:
3

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。