📌  相关文章
📜  打印将 N 写成两个或多个正整数之和的所有可能方式

📅  最后修改于: 2021-10-26 06:48:17             🧑  作者: Mango

给定一个整数N ,任务是打印N可以写成两个或多个正整数之和的所有可能方式。
例子:

方法:思路是用递归来解决这个问题。这个想法是考虑从1 到 N 的每个整数,这样总和 N 可以在每次递归调用时减少这个数字,如果在任何递归调用中 N 减少到零,那么我们将打印存储在向量中的答案。下面是递归的步骤:

  1. 获取其总和必须分解为两个或多个正整数的数字N。
  2. 递归地从值1迭代到 N作为索引i
  • 基本情况:如果递归调用的值为0 ,则打印当前向量,因为这是将 N 分解为两个或多个正整数的方法之一。
if (n == 0)
    printVector(arr);
  • 递归调用:如果不满足基本情况,则从[i, N – i]递归迭代。将当前元素j推入向量(比如arr )并递归迭代下一个索引,在递归结束后弹出之前插入的元素j
for j in range[i, N]:
    arr.push_back(j);
    recursive_function(arr, j + 1, N - j);
    arr.pop_back(j);   

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to print the values stored
// in vector arr
void printVector(vector& arr)
{
    if (arr.size() != 1) {
 
        // Traverse the vector arr
        for (int i = 0; i < arr.size(); i++) {
            cout << arr[i] << " ";
        }
        cout << endl;
    }
}
 
// Recursive function to print different
// ways in which N can be written as
// a sum of at 2 or more positive integers
void findWays(vector& arr, int i, int n)
{
    // If n is zero then print this
    // ways of breaking numbers
    if (n == 0)
        printVector(arr);
 
    // Start from previous element
    // in the representation till n
    for (int j = i; j <= n; j++) {
 
        // Include current element
        // from representation
        arr.push_back(j);
 
        // Call function again
        // with reduced sum
        findWays(arr, j, n - j);
 
        // Backtrack to remove current
        // element from representation
        arr.pop_back();
    }
}
 
// Driver Code
int main()
{
    // Given sum N
    int n = 4;
 
    // To store the representation
    // of breaking N
    vector arr;
 
    // Function Call
    findWays(arr, 1, n);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to print the values stored
// in vector arr
static void printVector(ArrayList arr)
{
    if (arr.size() != 1)
    {
         
        // Traverse the vector arr
        for(int i = 0; i < arr.size(); i++)
        {
            System.out.print(arr.get(i) + " ");
        }
        System.out.println();
    }
}
 
// Recursive function to print different
// ways in which N can be written as
// a sum of at 2 or more positive integers
static void findWays(ArrayList arr,
                     int i, int n)
{
     
    // If n is zero then print this
    // ways of breaking numbers
    if (n == 0)
        printVector(arr);
 
    // Start from previous element
    // in the representation till n
    for(int j = i; j <= n; j++)
    {
         
        // Include current element
        // from representation
        arr.add(j);
 
        // Call function again
        // with reduced sum
        findWays(arr, j, n - j);
 
        // Backtrack to remove current
        // element from representation
        arr.remove(arr.size() - 1);
    }
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given sum N
    int n = 4;
     
    // To store the representation
    // of breaking N
    ArrayList arr = new ArrayList();
     
    // Function call
    findWays(arr, 1, n);
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program for the above approach
 
# Function to print the values stored
# in vector arr
def printVector(arr):
 
    if (len(arr) != 1):
 
        # Traverse the vector arr
        for i in range(len(arr)):
            print(arr[i], end = " ")
        print()   
 
# Recursive function to prdifferent
# ways in which N can be written as
# a sum of at 2 or more positive integers
def findWays(arr, i, n):
 
    # If n is zero then prthis
    # ways of breaking numbers
    if (n == 0):
        printVector(arr)
 
    # Start from previous element
    # in the representation till n
    for j in range(i, n + 1):
 
        # Include current element
        # from representation
        arr.append(j)
 
        # Call function again
        # with reduced sum
        findWays(arr, j, n - j)
 
        # Backtrack to remove current
        # element from representation
        del arr[-1]
         
# Driver Code
if __name__ == '__main__':
 
    # Given sum N
    n = 4
 
    # To store the representation
    # of breaking N
    arr = []
 
    # Function Call
    findWays(arr, 1, n)
 
# 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 print the values stored
// in vector arr
static void printList(List arr)
{
    if (arr.Count != 1)
    {
         
        // Traverse the vector arr
        for(int i = 0; i < arr.Count; i++)
        {
            Console.Write(arr[i] + " ");
        }
        Console.WriteLine();
    }
}
 
// Recursive function to print different
// ways in which N can be written as
// a sum of at 2 or more positive integers
static void findWays(List arr,
                     int i, int n)
{
     
    // If n is zero then print this
    // ways of breaking numbers
    if (n == 0)
        printList(arr);
 
    // Start from previous element
    // in the representation till n
    for(int j = i; j <= n; j++)
    {
         
        // Include current element
        // from representation
        arr.Add(j);
 
        // Call function again
        // with reduced sum
        findWays(arr, j, n - j);
 
        // Backtrack to remove current
        // element from representation
        arr.RemoveAt(arr.Count - 1);
    }
}
 
// Driver code
public static void Main(String[] args)
{
     
    // Given sum N
    int n = 4;
     
    // To store the representation
    // of breaking N
    List arr = new List();
     
    // Function call
    findWays(arr, 1, n);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


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

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