Just recently updated the permutation problem, incidentally, the composition issue is also published
1. Description of the problem:
It is known to have a M ball and how many different ways to take n balls from M balls.
2. Input Example:
Please enter the total number of balls and the number of balls to be taken.
6 5
3. Sample output:
There are 6 kinds of extraction
4. code example:
Import Java.util.Scanner;
Known to have a M ball, from which to take n balls, how many different kinds of extraction
public class Quqiu {
public static void Main (string[] args) {
Scanner SCN = new Scanner (system.in);
System.out.println ("Please enter the total number of balls and the number of balls to be taken");
int m = Scn.nextint ();
int n = scn.nextint ();
int sum = f (m,n);//This method takes n balls from m balls and returns the number of ball-taking schemes.
SYSTEM.OUT.PRINTLN ("total" + sum + "seed extraction");
}
public static int f (int m, int n) {
if (m==n) {
return 1;
}
if (n==0) {
return 1;
}
Suppose there is a special ball, there are two cases, from the rest of the ball in addition to the special ball, take n ball, the other must not take a special ball
return f (m-1,n) +f (m-1,n-1);
}
}
Java Implementation Composition issues