📜  N个数字的计数,其每K个连续数字之和等于

📅  最后修改于: 2021-05-04 21:15:26             🧑  作者: Mango

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

例子:

天真的方法:迭代所有可能的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
        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 
#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 
#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

Java脚本


输出:
9

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