📜  将一个数字拆分为K个不可被K整除的数字之和

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

给定两个数字NK ,任务是将该数字拆分为K个正整数,以使它们的和等于N,并且这K个整数都不是K的倍数。
注意: N> = 2
例子:

方法:
要将N分解为K个数,我们需要通过以下步骤解决该问题:

  • 检查N是否可被K整除。
  • 如果N可被K整除,则N – K + 1无法被K整除。因此,我们可以将N分成N – K + 1的一部分,其余的K – 1全部为1
  • 如果N不能被K整除:
    • 如果K为2,则无法拆分。
    • 否则,将N分为K-2部分(全部为12),以及将N – K分为其余的两个部分。

下面的代码是上述方法的实现:

C++
// C++ program to split a number
// as sum of K numbers which are
// not divisible by K
 
#include 
using namespace std;
 
// Function to split into K parts
// and print them
void printKParts(int N, int K)
{
    if (N % K == 0) {
 
        // Print 1 K - 1 times
        for (int i = 1; i < K; i++)
            cout << "1, ";
 
        // Print N - K + 1
        cout << N - (K - 1) << endl;
    }
    else {
        if (K == 2) {
            cout << "Not Possible" << endl;
            return;
        }
 
        // Print 1 K-2 times
        for (int i = 1; i < K - 1; i++)
            cout << 1 << ", ";
 
        // Print 2 and N - K
        cout << 2 << ", "
             << N - K << endl;
    }
}
 
// Driver code
int main()
{
    int N = 18, K = 5;
    printKParts(N, K);
    return 0;
}


Java
// Java program to split a number
// as sum of K numbers which are
// not divisible by K
class GFG{
 
// Function to split into K parts
// and print them
static void printKParts(int N, int K)
{
    if (N % K == 0)
    {
         
        // Print 1 K - 1 times
        for(int i = 1; i < K; i++)
           System.out.print("1, ");
 
        // Print N - K + 1
        System.out.print(N - (K - 1) + "\n");
    }
    else
    {
        if (K == 2)
        {
            System.out.print("Not Possible" + "\n");
            return;
        }
 
        // Print 1 K-2 times
        for(int i = 1; i < K - 1; i++)
           System.out.print(1 + ", ");
 
        // Print 2 and N - K
        System.out.print(2 + ", " + (N - K) + "\n");
    }
}
 
// Driver code
public static void main(String[] args)
{
    int N = 18, K = 5;
     
    printKParts(N, K);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to split a number
# as sum of K numbers which are
# not divisible by K
 
# Function to split into K parts
# and print them
def printKParts(N, K):
     
    if (N % K == 0):
 
        # Print 1 K - 1 times
        for i in range(1, K):
            print("1, ");
 
        # Print N - K + 1
        print(N - (K - 1), end = "");
    else:
        if (K == 2):
            print("Not Possible", end = "");
            return;
             
    # Print 1 K-2 times
    for i in range(1, K - 1):
        print(1, end = ", ");
 
    # Print 2 and N - K
    print(2, ", ", (N - K), end = "");
 
# Driver code
if __name__ == '__main__':
     
    N = 18;
    K = 5;
 
    printKParts(N, K);
 
# This code is contributed by Rohit_ranjan


C#
// C# program to split a number
// as sum of K numbers which are
// not divisible by K
using System;
 
class GFG{
 
// Function to split into K parts
// and print them
static void printKParts(int N, int K)
{
    if (N % K == 0)
    {
         
        // Print 1 K - 1 times
        for(int i = 1; i < K; i++)
           Console.Write("1, ");
 
        // Print N - K + 1
        Console.Write(N - (K - 1) + "\n");
    }
    else
    {
        if (K == 2)
        {
            Console.Write("Not Possible" + "\n");
            return;
        }
 
        // Print 1 K-2 times
        for(int i = 1; i < K - 1; i++)
           Console.Write(1 + ", ");
 
        // Print 2 and N - K
        Console.Write(2 + ", " + (N - K) + "\n");
    }
}
 
// Driver code
public static void Main(String[] args)
{
    int N = 18, K = 5;
     
    printKParts(N, K);
}
}
 
// This code is contributed by sapnasingh4991


Javascript


输出:
1, 1, 1, 2, 13

时间复杂度: O(K)