Java Implementation Composition issues

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.