📜  打印通过重复掷骰子获得给定总和的方法

📅  最后修改于: 2022-05-13 01:56:04.911000             🧑  作者: Mango

打印通过重复掷骰子获得给定总和的方法

给定一个整数N ,任务是打印通过重复掷骰子得到总和N的方法。

方法:这个问题可以通过使用递归和回溯来解决。这个想法是在[1, 6]范围内迭代骰子i的每个可能值,并递归调用剩余的总和,即 ( N - i ),并继续将当前骰子值的值附加到字符串等数据结构中.如果所需的总和为零,则打印存储的字符串中的元素。

下面是上述方法的实现

C++
// C++ program of the above approach
#include 
using namespace std;
 
// Recursive function to print the
// number of ways to get the sum
// N with repeated throw of a dice
void printWays(int n, string ans)
{
    // Base Case
    if (n == 0) {
 
        // Print characters in
        // the string
        for (auto x : ans) {
            cout << x << " ";
        }
        cout << endl;
        return;
    }
 
    // If n is less than zero,
    // no sum is possible
    else if (n < 0) {
        return;
    }
 
    // Loop to iterate over all
    // the possible current moves
    for (int i = 1; i <= 6; i++) {
 
        // Recursive call for the
        // remaining sum considering
        // i as the current integer
        printWays(n - i, ans + to_string(i));
    }
}
 
// Driver Code
int main()
{
    int N = 3;
    printWays(N, "");
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
public class GFG {
     
// Recursive function to print the
// number of ways to get the sum
// N with repeated throw of a dice
static void printWays(int n, String ans)
{
   
    // Base Case
    if (n == 0) {
 
        // Print characters in
        // the string
        for (int i = 0; i < ans.length(); i++) {
            System.out.print(ans.charAt(i) + " ");
        }
        System.out.println();
        return;
    }
 
    // If n is less than zero,
    // no sum is possible
    else if (n < 0) {
        return;
    }
 
    // Loop to iterate over all
    // the possible current moves
    for (int i = 1; i <= 6; i++) {
 
        // Recursive call for the
        // remaining sum considering
        // i as the current integer
        printWays(n - i, ans + Integer.toString(i));
    }
}
 
// Driver Code
public static void main(String args[])
{
    int N = 3;
    printWays(N, "");
 
}
}
 
// This code is contributed by Samim Hossain Mondal.


Python3
# Python code for the above approach
 
# Recursive function to print the
# number of ways to get the sum
# N with repeated throw of a dice
def printWays(n, ans):
 
    # Base Case
    if n == 0:
 
        # Print characters in
        # the string
        for x in range(len(ans)):
            print(ans[x], end=" ")
        print("")
        return
 
    # If n is less than zero,
    # no sum is possible
    elif n < 0:
        return
 
    # Loop to iterate over all
    # the possible current moves
    for i in range(1, 7):
 
        # Recursive call for the
        # remaining sum considering
        # i as the current integer
        printWays(n - i, ans + str(i))
 
# Driver Code
N = 3
printWays(N, "")
 
# This code is contributed by gfgking


C#
// C# program for the above approach
using System;
using System.Collections;
 
class GFG {
 
  // Recursive function to print the
  // number of ways to get the sum
  // N with repeated throw of a dice
  static void printWays(int n, string ans)
  {
 
    // Base Case
    if (n == 0) {
 
      // Print characters in
      // the string
      for (int i = 0; i < ans.Length; i++) {
        Console.Write(ans[i] + " ");
      }
      Console.WriteLine();
      return;
    }
 
    // If n is less than zero,
    // no sum is possible
    else if (n < 0) {
      return;
    }
 
    // Loop to iterate over all
    // the possible current moves
    for (int i = 1; i <= 6; i++) {
 
      // Recursive call for the
      // remaining sum considering
      // i as the current integer
      printWays(n - i, ans + i.ToString());
    }
  }
 
  // Driver Code
  public static void Main()
  {
    int N = 3;
    printWays(N, "");
 
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
1 1 1 
1 2 
2 1 
3 

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