📜  Count of N digit 每K个连续数字之和相等的数

📅  最后修改于: 2021-10-25 10:33:20             🧑  作者: Mango

给定两个整数NK ,任务是找到N位数字的总数,使得该数字的每K个连续数字的总和相等。

例子:

朴素方法:迭代所有可能的N 位数字并计算该数字的每K个连续数字的总和。如果所有的总和都相等,则包括这是计数,否则检查下一个数字。

下面是上述方法的实现:

C++
// C++ program for
// the above approach
 
#include
using namespace std;
 
// Function to count the number of
// N-digit numbers such that sum of
// every k consecutive digits are equal
int countDigitSum(int N, int K)
{
      
    // Range of numbers
    int l = (int)pow(10, N - 1),
        r = (int)pow(10, N) - 1;
    int count = 0;
  
    for(int i = l; i <= r; i++)
    {
        int num = i;
  
        // Extract digits of the number
        int digits[N];
  
        for(int j = N - 1; j >= 0; j--)
        {
            digits[j] = num % 10;
            num /= 10;
        }
        int sum = 0, flag = 0;
  
        // Store the sum of first K digits
        for(int j = 0; j < K; j++)
            sum += digits[j];
  
        // Check for every
        // k-consective digits
        for(int j = 1; j < N - K + 1; j++)
        {
            int curr_sum = 0;
  
            for(int m = j; m < j + K; m++)
                    curr_sum += digits[m];
  
            // If sum is not equal
            // then break the loop
            if (sum != curr_sum)
            {
                flag = 1;
                break;
            }
        }
  
        // Increment the count if it
        // satisfy the given condition
        if (flag == 0)
        {
            count++;
        }
    }
    return count;
}
 
// Driver Code
int main()
{
     
    // Given N and K
    int N = 2, K = 1;
  
    // Function call
    cout << countDigitSum(N, K);
    return 0;
}
 
// This code is contributed by target_2.


C
// C program for the above approach
#include 
#include 
 
// Function to count the number of
// N-digit numbers such that sum of
// every k consecutive digits are equal
int countDigitSum(int N, int K)
{
     
    // Range of numbers
    int l = (int)pow(10, N - 1),
        r = (int)pow(10, N) - 1;
    int count = 0;
 
    for(int i = l; i <= r; i++)
    {
        int num = i;
 
        // Extract digits of the number
        int digits[N];
 
        for(int j = N - 1; j >= 0; j--)
        {
            digits[j] = num % 10;
            num /= 10;
        }
        int sum = 0, flag = 0;
 
        // Store the sum of first K digits
        for(int j = 0; j < K; j++)
            sum += digits[j];
 
        // Check for every
        // k-consective digits
        for(int j = 1; j < N - K + 1; j++)
        {
            int curr_sum = 0;
 
            for(int m = j; m < j + K; m++)
                    curr_sum += digits[m];
 
            // If sum is not equal
            // then break the loop
            if (sum != curr_sum)
            {
                flag = 1;
                break;
            }
        }
 
        // Increment the count if it
        // satisfy the given condition
        if (flag == 0)
        {
            count++;
        }
    }
    return count;
}
 
// Driver code
int main()
{
     
    // Given N and K
    int N = 2, K = 1;
 
    // Function call
    printf("%d", countDigitSum(N, K));
     
    return 0;
}
 
// This code is contributed by piyush3010


Java
// Java program for the above approach
 
class GFG {
 
    // Function to count the number of
    // N-digit numbers such that sum of
    // every k consecutive digits are equal
    static int countDigitSum(int N, int K)
    {
        // Range of numbers
        int l = (int)Math.pow(10, N - 1),
            r = (int)Math.pow(10, N) - 1;
        int count = 0;
 
        for (int i = l; i <= r; i++) {
            int num = i;
 
            // Extract digits of
            // the number
            int digits[] = new int[N];
 
            for (int j = N - 1;
                 j >= 0; j--) {
 
                digits[j] = num % 10;
                num /= 10;
            }
            int sum = 0, flag = 0;
 
            // Store the sum of
            // first K digits
            for (int j = 0; j < K; j++)
                sum += digits[j];
 
            // Check for every
            // k-consective digits
            for (int j = 1;
                 j < N - K + 1; j++) {
 
                int curr_sum = 0;
 
                for (int m = j;
                     m < j + K; m++) {
 
                    curr_sum += digits[m];
                }
 
                // If sum is not equal
                // then break the loop
                if (sum != curr_sum) {
                    flag = 1;
                    break;
                }
            }
 
            // Increment the count if it
            // satisfy the given condition
            if (flag == 0) {
                count++;
            }
        }
 
        return count;
    }
 
    // Driver Code
    public static void
        main(String[] args)
    {
        // Given N and K
        int N = 2, K = 1;
 
        // Function call
        System.out.print(countDigitSum(N, K));
    }
}


Python3
# Python3 program for the above approach
 
# Function to count the number of
# N-digit numbers such that sum of
# every k consecutive digits are equal
def countDigitSum(N, K):
     
    # Range of numbers
    l = pow(10, N - 1)
    r = pow(10, N) - 1
    count = 0
     
    for i in range(l, r + 1):
        num = i
 
        # Extract digits of the number
        digits = [0] * N
         
        for j in range(N - 1, -1, -1):
            digits[j] = num % 10
            num //= 10
         
        sum = 0
        flag = 0
 
        # Store the sum of first K digits
        for j in range(0, K):
            sum += digits[j]
 
        # Check for every
        # k-consective digits
        for j in range(1, N - K + 1):
            curr_sum = 0
             
            for m in range(j, j + K):
                    curr_sum += digits[m]
 
            # If sum is not equal
            # then break the loop
            if (sum != curr_sum):
                flag = 1
                break
         
        # Increment the count if it
        # satisfy the given condition
        if (flag == 0):
            count += 1
         
    return count
 
# Driver code
 
# Given N and K
N = 2
K = 1
 
# Function call
print(countDigitSum(N, K))
 
# This code is contributed by sanjoy_62


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to count the number of
// N-digit numbers such that sum of
// every k consecutive digits are equal
static int countDigitSum(int N, int K)
{
     
    // Range of numbers
    int l = (int)Math.Pow(10, N - 1),
        r = (int)Math.Pow(10, N) - 1;
         
    int count = 0;
 
    for(int i = l; i <= r; i++)
    {
        int num = i;
 
        // Extract digits of
        // the number
        int[] digits = new int[N];
 
        for(int j = N - 1; j >= 0; j--)
        {
            digits[j] = num % 10;
            num /= 10;
        }
        int sum = 0, flag = 0;
 
        // Store the sum of
        // first K digits
        for(int j = 0; j < K; j++)
            sum += digits[j];
 
        // Check for every
        // k-consective digits
        for(int j = 1; j < N - K + 1; j++)
        {
            int curr_sum = 0;
 
            for(int m = j; m < j + K; m++)
            {
                curr_sum += digits[m];
            }
 
            // If sum is not equal
            // then break the loop
            if (sum != curr_sum)
            {
                flag = 1;
                break;
            }
        }
 
        // Increment the count if it
        // satisfy the given condition
        if (flag == 0)
        {
            count++;
        }
    }
    return count;
}
 
// Driver Code
public static void Main()
{
     
    // Given N and K
    int N = 2, K = 1;
 
    // Function call
    Console.Write(countDigitSum(N, K));
}
}
 
// This code is contributed by sanjoy_62


Javascript


C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count the number of
// N-digit numbers such that sum of
// every k consecutive digits are equal
int countDigitSum(int N, int K)
{
     
    // Range of numbers
    int l = (int)pow(10, N - 1),
        r = (int)pow(10, N) - 1;
     
    int count = 0;
    for(int i = l; i <= r; i++)
    {
        int num = i;
 
        // Extract digits of the number
        int digits[N];
        for (int j = N - 1; j >= 0; j--)
        {
            digits[j] = num % 10;
            num /= 10;
        }
        int sum = 0, flag = 0;
 
        // Store the sum of first K digits
        for(int j = 0; j < K; j++)
            sum += digits[j];
 
        // Check for every
        // k-consective digits
        // using sliding window
        for(int j = K; j < N; j++)
        {
            if(sum - digits[j - K] +
                     digits[j] != sum)
            {
                flag = 1;
                break;
            }
        }
        if (flag == 0)
            count++;
    }
    return count;
}
 
// Driver Code
int main()
{
     
    // Given integer N and K
    int N = 2, K = 1;
     
    cout<< countDigitSum(N, K)<


C
// C program for the above approach
#include 
#include 
 
// Function to count the number of
// N-digit numbers such that sum of
// every k consecutive digits are equal
int countDigitSum(int N, int K)
{
     
    // Range of numbers
    int l = (int)pow(10, N - 1),
        r = (int)pow(10, N) - 1;
     
    int count = 0;
    for(int i = l; i <= r; i++)
    {
        int num = i;
 
        // Extract digits of the number
        int digits[N];
        for (int j = N - 1; j >= 0; j--)
        {
            digits[j] = num % 10;
            num /= 10;
        }
        int sum = 0, flag = 0;
 
        // Store the sum of first K digits
        for(int j = 0; j < K; j++)
            sum += digits[j];
 
        // Check for every
        // k-consective digits
        // using sliding window
        for(int j = K; j < N; j++)
        {
            if(sum - digits[j - K] +
                     digits[j] != sum)
            {
                flag = 1;
                break;
            }
        }
        if (flag == 0)
            count++;
    }
    return count;
}
 
// Driver Code
int main()
{
     
    // Given integer N and K
    int N = 2, K = 1;
     
    printf("%d", countDigitSum(N, K));
     
    return 0;
}
 
// This code is contributed by piyush3010


Java
// Java program for the above approach
class GFG {
 
    // Function to count the number of
    // N-digit numbers such that sum of
    // every k consecutive digits are equal
    static int countDigitSum(int N, int K)
    {
        // Range of numbers
        int l = (int)Math.pow(10, N - 1),
            r = (int)Math.pow(10, N) - 1;
        int count = 0;
        for (int i = l; i <= r; i++) {
            int num = i;
 
            // Extract digits of the number
            int digits[] = new int[N];
            for (int j = N - 1; j >= 0; j--) {
                digits[j] = num % 10;
                num /= 10;
            }
            int sum = 0, flag = 0;
 
            // Store the sum of
            // first K digits
            for (int j = 0; j < K; j++)
                sum += digits[j];
 
            // Check for every
            // k-consective digits
            // using sliding window
            for (int j = K; j < N; j++) {
 
                if (sum - digits[j - K]
                        + digits[j]
                    != sum) {
                    flag = 1;
                    break;
                }
            }
            if (flag == 0) {
                count++;
            }
        }
        return count;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given integer N and K
        int N = 2, K = 1;
        System.out.print(countDigitSum(N, K));
    }
}
 
/* This code is contributed by piyush3010 */


Python3
# Python3 program for the
# above approach
 
# Function to count the
# number of N-digit numbers
# such that sum of every k
# consecutive digits are equal
def countDigitSum(N, K):
   
    # Range of numbers
    l = pow(10, N - 1);
    r = pow(10, N) - 1;
    count = 0;
     
    for i in range(1, r + 1):
        num = i;
 
        # Extract digits of
        # the number
        digits = [0] * (N);
         
        for j in range(N - 1,
                       0, -1):
            digits[j] = num % 10;
            num //= 10;
 
        sum = 0;
        flag = 0;
 
        # Store the sum of
        # first K digits
        for j in range(0, K):
            sum += digits[j];
 
        # Check for every
        # k-consective digits
        # using sliding window
        for j in range(K, N):
            if (sum - digits[j - K] +
                digits[j] != sum):
                flag = 1;
                break;
 
        if (flag == 0):
            count += 1;
 
    return count;
 
# Driver Code
if __name__ == '__main__':
   
    # Given integer N and K
    N = 2;
    K = 1;
    print(countDigitSum(N, K));
 
# This code is contributed by shikhasingrajput


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to count the number of
// N-digit numbers such that sum of
// every k consecutive digits are equal
static int countDigitSum(int N, int K)
{
     
    // Range of numbers
    int l = (int)Math.Pow(10, N - 1),
        r = (int)Math.Pow(10, N) - 1;
    int count = 0;
     
    for(int i = l; i <= r; i++)
    {
        int num = i;
 
        // Extract digits of the number
        int[] digits = new int[N];
        for(int j = N - 1; j >= 0; j--)
        {
            digits[j] = num % 10;
            num /= 10;
        }
        int sum = 0, flag = 0;
 
        // Store the sum of
        // first K digits
        for(int j = 0; j < K; j++)
            sum += digits[j];
 
        // Check for every
        // k-consective digits
        // using sliding window
        for(int j = K; j < N; j++)
        {
            if (sum - digits[j - K] +
                      digits[j] != sum)
            {
                flag = 1;
                break;
            }
        }
        if (flag == 0)
        {
            count++;
        }
    }
    return count;
}
 
// Driver Code
public static void Main()
{
     
    // Given N and K
    int N = 2, K = 1;
 
    // Function call
    Console.Write(countDigitSum(N, K));
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出:
9

时间复杂度: O(10 N * N * K)
辅助空间: O(N)

有效方法:为了优化上述简单方法,其思想是使用滑动窗口技术来检查数字的 K 连续数字之和是否相等。以下是步骤:

  1. 获取数字范围,即10 N-1到 10 N
  2. 对于上述范围内的每个数字,考虑一个长度为K的窗口并找到每个数字的总和。将此总和存储为S
  3. 通过在总和中包含接下来的K位数字并从总和中删除前K位数字,使用滑动窗口找到接下来的K位数字的总和。
  4. 如果获得的总和等于上述总和S,则检查接下来的K位数字。
  5. 否则,对下一个数字重复上述步骤。

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
using namespace std;
 
// Function to count the number of
// N-digit numbers such that sum of
// every k consecutive digits are equal
int countDigitSum(int N, int K)
{
     
    // Range of numbers
    int l = (int)pow(10, N - 1),
        r = (int)pow(10, N) - 1;
     
    int count = 0;
    for(int i = l; i <= r; i++)
    {
        int num = i;
 
        // Extract digits of the number
        int digits[N];
        for (int j = N - 1; j >= 0; j--)
        {
            digits[j] = num % 10;
            num /= 10;
        }
        int sum = 0, flag = 0;
 
        // Store the sum of first K digits
        for(int j = 0; j < K; j++)
            sum += digits[j];
 
        // Check for every
        // k-consective digits
        // using sliding window
        for(int j = K; j < N; j++)
        {
            if(sum - digits[j - K] +
                     digits[j] != sum)
            {
                flag = 1;
                break;
            }
        }
        if (flag == 0)
            count++;
    }
    return count;
}
 
// Driver Code
int main()
{
     
    // Given integer N and K
    int N = 2, K = 1;
     
    cout<< countDigitSum(N, K)<

C

// C program for the above approach
#include 
#include 
 
// Function to count the number of
// N-digit numbers such that sum of
// every k consecutive digits are equal
int countDigitSum(int N, int K)
{
     
    // Range of numbers
    int l = (int)pow(10, N - 1),
        r = (int)pow(10, N) - 1;
     
    int count = 0;
    for(int i = l; i <= r; i++)
    {
        int num = i;
 
        // Extract digits of the number
        int digits[N];
        for (int j = N - 1; j >= 0; j--)
        {
            digits[j] = num % 10;
            num /= 10;
        }
        int sum = 0, flag = 0;
 
        // Store the sum of first K digits
        for(int j = 0; j < K; j++)
            sum += digits[j];
 
        // Check for every
        // k-consective digits
        // using sliding window
        for(int j = K; j < N; j++)
        {
            if(sum - digits[j - K] +
                     digits[j] != sum)
            {
                flag = 1;
                break;
            }
        }
        if (flag == 0)
            count++;
    }
    return count;
}
 
// Driver Code
int main()
{
     
    // Given integer N and K
    int N = 2, K = 1;
     
    printf("%d", countDigitSum(N, K));
     
    return 0;
}
 
// This code is contributed by piyush3010

Java

// Java program for the above approach
class GFG {
 
    // Function to count the number of
    // N-digit numbers such that sum of
    // every k consecutive digits are equal
    static int countDigitSum(int N, int K)
    {
        // Range of numbers
        int l = (int)Math.pow(10, N - 1),
            r = (int)Math.pow(10, N) - 1;
        int count = 0;
        for (int i = l; i <= r; i++) {
            int num = i;
 
            // Extract digits of the number
            int digits[] = new int[N];
            for (int j = N - 1; j >= 0; j--) {
                digits[j] = num % 10;
                num /= 10;
            }
            int sum = 0, flag = 0;
 
            // Store the sum of
            // first K digits
            for (int j = 0; j < K; j++)
                sum += digits[j];
 
            // Check for every
            // k-consective digits
            // using sliding window
            for (int j = K; j < N; j++) {
 
                if (sum - digits[j - K]
                        + digits[j]
                    != sum) {
                    flag = 1;
                    break;
                }
            }
            if (flag == 0) {
                count++;
            }
        }
        return count;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given integer N and K
        int N = 2, K = 1;
        System.out.print(countDigitSum(N, K));
    }
}
 
/* This code is contributed by piyush3010 */

蟒蛇3

# Python3 program for the
# above approach
 
# Function to count the
# number of N-digit numbers
# such that sum of every k
# consecutive digits are equal
def countDigitSum(N, K):
   
    # Range of numbers
    l = pow(10, N - 1);
    r = pow(10, N) - 1;
    count = 0;
     
    for i in range(1, r + 1):
        num = i;
 
        # Extract digits of
        # the number
        digits = [0] * (N);
         
        for j in range(N - 1,
                       0, -1):
            digits[j] = num % 10;
            num //= 10;
 
        sum = 0;
        flag = 0;
 
        # Store the sum of
        # first K digits
        for j in range(0, K):
            sum += digits[j];
 
        # Check for every
        # k-consective digits
        # using sliding window
        for j in range(K, N):
            if (sum - digits[j - K] +
                digits[j] != sum):
                flag = 1;
                break;
 
        if (flag == 0):
            count += 1;
 
    return count;
 
# Driver Code
if __name__ == '__main__':
   
    # Given integer N and K
    N = 2;
    K = 1;
    print(countDigitSum(N, K));
 
# This code is contributed by shikhasingrajput

C#

// C# program for the above approach
using System;
 
class GFG{
 
// Function to count the number of
// N-digit numbers such that sum of
// every k consecutive digits are equal
static int countDigitSum(int N, int K)
{
     
    // Range of numbers
    int l = (int)Math.Pow(10, N - 1),
        r = (int)Math.Pow(10, N) - 1;
    int count = 0;
     
    for(int i = l; i <= r; i++)
    {
        int num = i;
 
        // Extract digits of the number
        int[] digits = new int[N];
        for(int j = N - 1; j >= 0; j--)
        {
            digits[j] = num % 10;
            num /= 10;
        }
        int sum = 0, flag = 0;
 
        // Store the sum of
        // first K digits
        for(int j = 0; j < K; j++)
            sum += digits[j];
 
        // Check for every
        // k-consective digits
        // using sliding window
        for(int j = K; j < N; j++)
        {
            if (sum - digits[j - K] +
                      digits[j] != sum)
            {
                flag = 1;
                break;
            }
        }
        if (flag == 0)
        {
            count++;
        }
    }
    return count;
}
 
// Driver Code
public static void Main()
{
     
    // Given N and K
    int N = 2, K = 1;
 
    // Function call
    Console.Write(countDigitSum(N, K));
}
}
 
// This code is contributed by sanjoy_62

Javascript


输出:
9

时间复杂度: O(10 N *N)
辅助空间: O(N)