📌  相关文章
📜  形成具有最大 K 的数字的方法数

📅  最后修改于: 2021-09-24 03:19:14             🧑  作者: Mango

给定数目N和一个数字K,任务是计算的方式的数量以形成具有Ks的在它的最大数量,其中,在操作中,N的两个相邻数字该总和最多K均是替换为数字

例子

方法:以下观察有助于解决问题:

  1. 将数字视为字符串,形式为“ABAB…”的子串,其中A+B=K是数字中唯一对答案有贡献的部分。
  2. 如果贡献子串的长度是偶数,则只有一种方法可以对它们应用操作,因此它们对答案没有贡献。例如,如果子串是“ABAB” ,就只能改成“KK” ,得到最终数中K的最大个数
  3. 否则,将有⌈L/2⌉方法来获得Ks的最大数量,其中L是子串的长度。例如,如果子字符串是“ABABA” ,则可以将其转换为以下内容:
    1. “KKA”
    2. “卡克”
    3. “AKK”

请按照以下步骤解决问题:

  1. N转换为字符串,例如S
  2. 将变量ans初始化为1 ,以存储最终答案。
  3. 遍历字符串S并对每个当前索引i执行以下操作:
    1. 将变量count初始化为1 ,以存储当前答案的长度。
    2. i小于S 的长度时循环,并且S[i]S[i-1]处的数字之和等于K ,并执行以下操作:
      1. 增加i
      2. 递增计数
    3. 如果计数为奇数,则将ans更新为ans*(count+1)/2。
  4. 返回ans

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
// Function to calculate number
// of ways a number can be
// formed that has the maximum number of Ks
int noOfWays(int N, int K)
{
    // convert to string
    string S = to_string(N);
    int ans = 1;
    // calculate length of subarrays
    // that can contribute to
    // the answer
    for (int i = 1; i < S.length(); i++) {
        int count = 1;
        // count length of subarray
        // where adjacent digits
        // add up to K
        while (i < S.length()
               && S[i] - '0' + S[i - 1] - '0' == K) {
            count++;
            i++;
        }
        // Current subarray can
        // contribute to the answer
        // only if it is odd
        if (count % 2)
            ans *= (count + 1) / 2;
    }
    // return the answer
    return ans;
}
// Driver code
int main()
{
    // Input
    int N = 1454781;
    int K = 9;
 
    // Function call
    cout << noOfWays(N, K) << endl;
}


Java
// Java program for tha above approach
import java.util.*;
 
class GFG{
     
// Function to calculate number
// of ways a number can be formed
// that has the maximum number of Ks
static int noOfWays(int N, int K)
{
     
    // Convert to string
    String S = String.valueOf(N);
    int ans = 1;
     
    // Calculate length of subarrays
    // that can contribute to
    // the answer
    for(int i = 1; i < S.length(); i++)
    {
        int count = 1;
         
        // Count length of subarray
        // where adjacent digits
        // add up to K
        while (i < S.length() && (int)S.charAt(i) - 48 +
              (int)S.charAt(i - 1) - 48 == K)
        {
            count++;
            i++;
        }
         
        // Current subarray can
        // contribute to the answer
        // only if it is odd
        if (count % 2 == 1)
            ans *= (count + 1) / 2;
    }
     
    // Return the answer
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Input
    int N = 1454781;
    int K = 9;
 
    // Function call
    System.out.print(noOfWays(N, K));
}   
}
 
// This code is contributed by susmitakundugoaldanga


Python3
# Python3 program for the above approach
 
# Function to calculate number of ways a
# number can be formed that has the
# maximum number of Ks
def noOfWays(N, K):
     
    # Convert to string
    S = str(N)
    ans = 1
     
    # Calculate length of subarrays
    # that can contribute to
    # the answer
    for i in range(1, len(S)):
        count = 1
         
        # Count length of subarray
        # where adjacent digits
        # add up to K
        while (i < len(S) and ord(S[i]) +
         ord(S[i - 1]) - 2 * ord('0') == K):
            count += 1
            i += 1
             
        # Current subarray can
        # contribute to the answer
        # only if it is odd
        if (count % 2):
            ans *= (count + 1) // 2
             
    # Return the answer
    return ans
     
# Driver code
if __name__ == '__main__':
     
    # Input
    N = 1454781
    K = 9
 
    # Function call
    print(noOfWays(N, K))
     
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
     
// Function to calculate number
// of ways a number can be
// formed that has the maximum number of Ks
static int noOfWays(int N, int K)
{
     
    // Convert to string
    string S = N.ToString();
    int ans = 1;
     
    // Calculate length of subarrays
    // that can contribute to
    // the answer
    for(int i = 1; i < S.Length; i++)
    {
        int count = 1;
         
        // Count length of subarray
        // where adjacent digits
        // add up to K
        while (i < S.Length && (int)S[i] - 48 +
              (int)S[i - 1] - 48 == K)
        {
            count++;
            i++;
        }
         
        // Current subarray can
        // contribute to the answer
        // only if it is odd
        if (count % 2 == 1)
            ans *= (count + 1) / 2;
    }
     
    // Return the answer
    return ans;
}
 
// Driver code
public static void Main()
{
     
    // Input
    int N = 1454781;
    int K = 9;
 
    // Function call
    Console.Write(noOfWays(N, K));
}
}
 
// This code is contributed by ipg2016107


Javascript


输出:
2

时间复杂度: O(Log 10 N),因为,N 中的位数是 Log 10 N
辅助空间: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程