Question:
Charles and the Necklace
Charles wants to buy a necklace in which.
1. There is a minimum of 1 pearl and maximum of X pearls, such that each
pearl has its own magnificent coefficient
2. The pearls should be in non-decreasing order of their magnificence power.
You are given the maximum number of pearls in a necklace and the range of
the magnificent coefficients of the pearls. Find the number of necklaces that
can be made that follow the mentioned conditions.
Answers
Answered by
5
Explanation:
import java.util.Scanner;
public class Main
{
static int c;
public static void main(String[] args)
{
c = 0;
System.out.print("Enter N : ");
int n = (new Scanner(System.in)).nextInt();
for (int i = 0; i < n; i++)
print(1, n, i, "");
System.out.println("The count is : " + c);
}
private static void print(int s, int n, int x, String p)
{
if (x == 0)
for (int i = s; i <= n; i++) {
System.out.println(p + i);
c++;
}
else
for (int i = s; i <= n - x; i++)
print(i + 1, n, x - 1, p + i + " ");
}
}
Similar questions