📌  相关文章
📜  通过最大化可被K整除的子部分的数量来拆分数字N

📅  最后修改于: 2021-06-26 20:07:38             🧑  作者: Mango

给定一个数字字符串N和一个整数K ,任务是将N的数字分成多个子部分,以使被K整除的段数达到最大。
注意:我们可以在成对的相邻数字之间进行任意数量的垂直切割。

例子:

方法:
为了解决上述问题,我们将尝试使用递归方法。

  • 检查当前字符和下一个字符之间是否存在垂直分区,如果没有,则对下一个索引再次执行递归,并通过串联当前字符更新子字符串值。
  • 现在,如果当前字符和下一个字符之间存在垂直分区,则存在两种情况:
    1. 如果当前subStr被X整除,则由于当前subStr是可能答案的1,所以我们加1,然后递归下一个索引并将subStr更新为空字符串。
    2. 如果当前的subStr不能被X整除,那么我们简单地递归下一个索引并将subStr更新为空字符串。
  • 最多返回上述两种可能的情况。

下面是上述逻辑的实现

C++
// C++ program to split the number N
// by maximizing the count
// of subparts divisible by K
 
#include 
using namespace std;
 
// Function to count the subparts
int count(string N, int X,
          string subStr,
          int index, int n)
{
 
    if (index == n)
        return 0;
 
    // Total subStr till now
    string a = subStr + N[index];
 
    // b marks the subString uptil now
    // is divisible by X or not,
 
    // If it can be divided,
    // then this substring is one
    // of the possible answer
    int b = 0;
 
    // Convert string to long long and
    // check if its divisible with X
    if (stoll(a) % X == 0)
        b = 1;
 
    // Consider there is no vertical
    // cut between this index and the
    // next one, hence take total
    // carrying total substr a.
    int m1 = count(N, X, a, index + 1, n);
 
    // If there is vertical
    // cut between this index
    // and next one, then we
    // start again with subStr as ""
    // and add b for the count
    // of subStr upto now
    int m2 = b + count(N, X, "",
                       index + 1, n);
 
    // Return max of both the cases
    return max(m1, m2);
}
 
// Driver code
int main()
{
    string N = "00001242";
 
    int K = 3;
 
    int l = N.length();
 
    cout << count(N, K, "", 0, l)
         << endl;
 
    return 0;
}


Java
// Java program to split the number N
// by maximizing the count
// of subparts divisible by K
class GFG{
 
// Function to count the subparts
static int count(String N, int X,
                 String subStr,
                 int index, int n)
{
    if (index == n)
        return 0;
 
    // Total subStr till now
    String a = subStr + N.charAt(index);
 
    // b marks the subString uptil now
    // is divisible by X or not,
 
    // If it can be divided,
    // then this subString is one
    // of the possible answer
    int b = 0;
 
    // Convert String to long and
    // check if its divisible with X
    if (Long.valueOf(a) % X == 0)
        b = 1;
 
    // Consider there is no vertical
    // cut between this index and the
    // next one, hence take total
    // carrying total substr a.
    int m1 = count(N, X, a, index + 1, n);
 
    // If there is vertical
    // cut between this index
    // and next one, then we
    // start again with subStr as ""
    // and add b for the count
    // of subStr upto now
    int m2 = b + count(N, X, "",
                       index + 1, n);
 
    // Return max of both the cases
    return Math.max(m1, m2);
}
 
// Driver code
public static void main(String[] args)
{
    String N = "00001242";
 
    int K = 3;
 
    int l = N.length();
 
    System.out.print(count(N, K, "", 0, l) + "\n");
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program to split the number N
# by maximizing the count
# of subparts divisible by K
 
# Function to count the subparts
def count(N, X, subStr, index, n):
     
    if (index == n):
        return 0
     
    # Total subStr till now
    a = subStr + N[index]
     
    # b marks the subString uptil now
    # is divisible by X or not,
 
    # If it can be divided,
    # then this substring is one
    # of the possible answer
    b = 0
 
    # Convert string to long long and
    # check if its divisible with X
    if (int(a) % X == 0):
        b = 1
     
    # Consider there is no vertical
    # cut between this index and the
    # next one, hence take total
    # carrying total substr a.
    m1 = count(N, X, a, index + 1, n)
 
    # If there is vertical
    # cut between this index
    # and next one, then we
    # start again with subStr as ""
    # and add b for the count
    # of subStr upto now
    m2 = b + count(N, X, "", index + 1, n)
 
    # Return max of both the cases
    return max(m1, m2)
     
# Driver code
N = "00001242"
K = 3
 
l = len(N)
 
print(count(N, K, "", 0, l))
 
# This code is contributed by sanjoy_62


C#
// C# program to split the number N
// by maximizing the count
// of subparts divisible by K
using System;
class GFG{
 
// Function to count the subparts
static int count(String N, int X,
                 String subStr,
                 int index, int n)
{
    if (index == n)
        return 0;
 
    // Total subStr till now
    String a = subStr + N[index];
 
    // b marks the subString uptil now
    // is divisible by X or not,
 
    // If it can be divided,
    // then this subString is one
    // of the possible answer
    int b = 0;
 
    // Convert String to long and
    // check if its divisible with X
    if (long. Parse(a) % X == 0)
        b = 1;
 
    // Consider there is no vertical
    // cut between this index and the
    // next one, hence take total
    // carrying total substr a.
    int m1 = count(N, X, a, index + 1, n);
 
    // If there is vertical
    // cut between this index
    // and next one, then we
    // start again with subStr as ""
    // and add b for the count
    // of subStr upto now
    int m2 = b + count(N, X, "",
                  index + 1, n);
 
    // Return max of both the cases
    return Math.Max(m1, m2);
}
 
// Driver code
public static void Main(String[] args)
{
    String N = "00001242";
 
    int K = 3;
 
    int l = N.Length;
 
    Console.Write(count(N, K, "", 0, l) + "\n");
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
6