Tri-Color Ball problem
If you put 12 balls in one pocket, 3 red, 3 yellow, 6 green, choose 8 from any of them, how many different combinations do you ask?
1: Analysis of the problem
The red ball can have 4 possibilities: 0, 1, 2, 3
The yellow ball can have 4 possibilities: 0, 1, 2, 3
Green balls can have 7 possibilities: 0, 1, 2, 3, 4, 5, 6
We just need to be poor and give every possible situation.
PackageTri-color ball;ImportJava.util.Scanner; Public classMain { Public Static voidMain (string[] args) {intRed,yellow,green;//stores the number of red, yellow, and green balls intN; System.out.println ("Three-color ball problem solving!" \ n "); System.out.println ("Please enter the number of red balls as:"); Scanner input=NewScanner (system.in); Red=Input.nextint (); System.out.println ("Please enter the number of yellow balls:"); Yellow=Input.nextint (); System.out.println ("Please enter the number of green balls:"); Green=Input.nextint (); System.out.println ("Please enter the number of balls to be removed first:"); N=Input.nextint (); //solvingThreeball (red,yellow,green,n); } Private Static voidThreeball (intRedintYellowintGreenintN) {inti,j,k; System.out.println ("There are a total of the following possibilities!" \ n "); System.out.printf ("\ t red ball \ t yellow ball \ t Green Ball"); for(i=0;i<=red;i++) { for(j=0;j<=yellow;j++) { for(k=0;k<=green;k++) { if(I+j+k = =N) {System.out.printf ("\t%d\t%d\t%d\n", i,j,k); } } } } }}
Results:
Three-color ball problem solving!
Please enter the number of red balls as:
3
Please enter the number of yellow balls:
3
Please enter the number of green balls:
6
Please enter the number of balls to be removed first:
8
There are a total of the following possibilities!
Red ball yellow ball green ball 026
035
116
125
134
206
215
224
233
305
314
323
332
Tri-Color Ball problem