📜  生成整数的所有唯一分区|套装2

📅  最后修改于: 2021-04-22 09:06:22             🧑  作者: Mango

给定一个正整数n ,任务是生成所有可能的唯一方式,将n表示为正整数之和。

例子:

方法:我们已经在本文中讨论了生成唯一分区的实现。这篇文章使用递归包含了另一个解决上述问题的直观方法。

这个想法很简单,并且与此处使用的方法相同。我们必须从n递归移动到1,并继续在数组中附加用于形成sum的数字。当总和等于n时,我们将打印该数组并返回。在实现中考虑的基本情况是: remSum == 0:形成了必需的n,因此打印数组。

然后,我们开始使用上一个分区中使用的最大值形成所需的总和。如果该数字大于n,我们将其忽略,否则我们将该数字附加到数组中,然后递归移至下一个迭代,形成sum =(remSum – i) ,其中最大值
可以使用的数字是i或小于i。这样,我们可以生成所需的唯一分区。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Array to store the numbers used
// to form the required sum
int dp[200];
int count = 0;
  
// Function to print the array which contains
// the unique partitions which are used
// to form the required sum
void print(int idx)
{
    for (int i = 1; i < idx; i++) {
        cout << dp[i] << " ";
    }
    cout << endl;
}
  
// Function to find all the unique partitions
// remSum = remaining sum to form
// maxVal is the maximum number that
// can be used to make the partition
void solve(int remSum, int maxVal, int idx, int& count)
{
  
    // If remSum == 0 that means the sum
    // is achieved so print the array
    if (remSum == 0) {
        print(idx);
        count++;
        return;
    }
  
    // i will begin from maxVal which is the
    // maximum value which can be used to form the sum
    for (int i = maxVal; i >= 1; i--) {
        if (i > remSum) {
            continue;
        }
        else if (i <= remSum) {
  
            // Store the number used in forming
            // sum gradually in the array
            dp[idx] = i;
  
            // Since i used the rest of partition
            // cant have any number greater than i
            // hence second parameter is i
            solve(remSum - i, i, idx + 1, count);
        }
    }
}
  
// Driver code
int main()
{
    int n = 4, count = 0;
  
    solve(n, n, 1, count);
  
    return 0;
}


Java
// Java implementation of the approach 
import java.util.*;
  
class GFG 
{
  
    // Array to store the numbers used 
    // to form the required sum 
    static int[] dp = new int[200];
    static int count = 0;
  
    // Function to print the array which contains 
    // the unique partitions which are used 
    // to form the required sum 
    static void print(int idx) 
    {
        for (int i = 1; i < idx; i++) 
        {
            System.out.print(dp[i] + " ");
        }
        System.out.println("");
    }
  
    // Function to find all the unique partitions 
    // remSum = remaining sum to form 
    // maxVal is the maximum number that 
    // can be used to make the partition 
    static void solve(int remSum, int maxVal, 
                        int idx, int count)
    {
  
        // If remSum == 0 that means the sum 
        // is achieved so print the array 
        if (remSum == 0) 
        {
            print(idx);
            count++;
            return;
        }
  
        // i will begin from maxVal which is the 
        // maximum value which can be used to form the sum 
        for (int i = maxVal; i >= 1; i--) 
        {
            if (i > remSum) 
            {
                continue;
            }
            else if (i <= remSum)
            {
  
                // Store the number used in forming 
                // sum gradually in the array 
                dp[idx] = i;
  
                // Since i used the rest of partition 
                // cant have any number greater than i 
                // hence second parameter is i 
                solve(remSum - i, i, idx + 1, count);
            }
        }
    }
  
    // Driver code 
    public static void main(String[] args)
    {
        int n = 4, count = 0;
  
        solve(n, n, 1, count);
    }
}
  
// This code has been contributed by 29AjayKumar


Python3
# Python 3 implementation of the approach
  
# Array to store the numbers used
# to form the required sum
dp = [0 for i in range(200)]
count = 0
  
# Function to print the array which contains
# the unique partitions which are used
# to form the required sum
def print1(idx):
    for i in range(1,idx,1):
        print(dp[i],end = " ")
    print("\n",end = "")
  
# Function to find all the unique partitions
# remSum = remaining sum to form
# maxVal is the maximum number that
# can be used to make the partition
def solve(remSum,maxVal,idx,count):
    # If remSum == 0 that means the sum
    # is achieved so print the array
    if (remSum == 0):
        print1(idx)
        count += 1
        return
    # i will begin from maxVal which is the
    # maximum value which can be used to form the sum
    i = maxVal
    while(i >= 1):
        if (i > remSum):
            i -= 1
            continue
        elif (i <= remSum):
            # Store the number used in forming
            # sum gradually in the array
            dp[idx] = i
  
            # Since i used the rest of partition
            # cant have any number greater than i
            # hence second parameter is i
            solve(remSum - i, i, idx + 1, count)
            i -= 1
  
# Driver code
if __name__ == '__main__':
    n = 4
    count = 0
  
    solve(n, n, 1, count)
      
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach 
using System;
  
class GFG 
{ 
  
    // Array to store the numbers used 
    // to form the required sum 
    static int[] dp = new int[200]; 
  
    // Function to print the array which contains 
    // the unique partitions which are used 
    // to form the required sum 
    static void print(int idx) 
    { 
        for (int i = 1; i < idx; i++) 
        { 
            Console.Write(dp[i] + " "); 
        } 
        Console.WriteLine(""); 
    } 
  
    // Function to find all the unique partitions 
    // remSum = remaining sum to form 
    // maxVal is the maximum number that 
    // can be used to make the partition 
    static void solve(int remSum, int maxVal, 
                        int idx, int count) 
    { 
  
        // If remSum == 0 that means the sum 
        // is achieved so print the array 
        if (remSum == 0) 
        { 
            print(idx); 
            count++; 
            return; 
        } 
  
        // i will begin from maxVal which is the 
        // maximum value which can be used to form the sum 
        for (int i = maxVal; i >= 1; i--) 
        { 
            if (i > remSum) 
            { 
                continue; 
            } 
            else if (i <= remSum) 
            { 
  
                // Store the number used in forming 
                // sum gradually in the array 
                dp[idx] = i; 
  
                // Since i used the rest of partition 
                // cant have any number greater than i 
                // hence second parameter is i 
                solve(remSum - i, i, idx + 1, count); 
            } 
        } 
    } 
  
    // Driver code 
    public static void Main() 
    { 
        int n = 4, count = 0; 
  
        solve(n, n, 1, count); 
    }
} 
  
// This code is contributed by AnkitRai01


输出:
4 
3 1 
2 2 
2 1 1 
1 1 1 1