Enter n arrays, array lengths, and each array takes a number to combine to find all the combinations.

Source: Internet
Author: User

Reprint declaration: The original text transferred from http://www.cnblogs.com/xiezie/p/5511707.html

Last night, a friend found me, he was using MATLAB programming, but encountered a problem, can not solve.

The problem is as follows:

Enter n arrays, array lengths, and a number from each array to combine to find all the combinations.

Example:

int a[]={1,2};

int b[]={3,4,5};

Possible combinations: {1,3};{1,4};{1,5};{2,3};{2,4};{2,5};

For about 40 minutes, fulfill ~

Java Code Implementation:

ImportJava.io.BufferedInputStream;Importjava.util.ArrayList;ImportJava.util.Scanner; Public classTest { Public Static voidMain (string[] args) {Scanner Scanner=NewScanner (NewBufferedinputstream (system.in));
The arraylist<arraylist<integer>> is used here to facilitate the operation of the int array ArrayList<ArrayList<Integer>> lists =NewArraylist<>();//If known, the specific number of combinations can define the capacity length---------"System.out.println"Enter the total number of all arrays:"); intn = scanner.nextint ();//Number of arrays int[] Alllen =New int[n];//the length of all arrays intindex = 0; intLL =N; while(LL--! =0) {System.out.println ("Enter" + (Index + 1) + "Length of the number of arrays:"); intm = Scanner.nextint ();//Enter the length of an arrayalllen[index++] =m; ArrayList<Integer> arrayList =NewArraylist<>(m); System.out.println ("Input" + index + "data:" + "number is \ t" +m); for(inti = 0; I! = m; i + +) {Arraylist.add (Scanner.nextint ()); } lists.add (ArrayList); } int[] Alllenproduct =New int[n];//the length product increment of an array intp = 1;//Number of combinations intL =N; intindex1 = 0; ArrayList<ArrayList<Integer>> results =NewArraylist<>(P); while(--l!=-1) {Alllenproduct[index1]=p; P=alllen[index1++]*p; } for(inti = 0; I! = P; i + +) {ArrayList<Integer> arrayList =NewArraylist<>(n); for(intj = 0; J! = N; J + +) {Arraylist.add (Lists.get (j). Get (I/alllenproduct[j]%alllen[j])); } results.add (ArrayList); } intLen =results.size (); System.out.println ("All the combinations:"); for(inti = 0; I! = Len; i + +) {ArrayList<Integer> arrayList =Results.get (i); intALen =arraylist.size (); System.out.print ("\ t \ t" + (i + 1) + "T-line: \ t"); for(intj = 0; J! = ALen; J + +) {System.out.print (Arraylist.get (j)+ " "); } System.out.println (); } scanner.close (); }}

Test:

Input:

Enter the total number of all arrays:
4
Enter the length of the 1th array:
2
Enter the data for the 1th array: number 2
1 2
Enter the length of the 2nd array:
3
Enter the data for the 2nd array: Number 3
4 5 6
Enter the length of the 3rd array:
3
Enter the data for the 3rd array: Number 3
7 8 9
Enter the length of the 4th array:
4
Enter the data for the 4th array: number 4
Ten

Results:

all the combinations:
line 1th: 1 4 7
Line 2nd: 2 4 7
Line 3rd: 1 5 7
Line 4th: 2 5 7
Line 5th: 1 6 7
Line 6th: 2 6 7
Line 7th: 1 4 8
Line 8th: 2 4 8
Line 9th: 1 5 8
Line 10th: 2 5 8
Line 11th: 1 6 8
Line 12th: 2 6 8
Line 13th: 1 4 9
Line 14th: 2 4 9
Line 15th: 1 5 9
Line 16th: 2 5 9
Line 17th: 1 6 9
Line 18th: 2 6 9
Line 19th: 1 4 7
Line 20th: 2 4 7
Line 21st: 1 5 7
Line 22nd: 2 5 7
Line 23rd: 1 6 7
Line 24th: 2 6 7
Line 25th: 1 4 8
Line 26th: 2 4 8
Line 27th: 1 5 8
Line 28th: 2 5 8
Line 29th: 1 6 8
Line 30th: 2 6 8
Line 31st: 1 4 9
Line 32nd: 2 4 9
Line 33rd: 1 5 9
Line 34th: 2 5 9
Line 35th: 1 6 9
Line 36th: 2 6 9
line 37th: 1 4 7
Line 38th: 2 4 7
Line 39th: 1 5 7
Line 40th: 2 5 7
Line 41st: 1 6 7
Line 42nd: 2 6 7
Line 43rd: 1 4 8
Line 44th: 2 4 8
Line 45th: 1 5 8
Line 46th: 2 5 8
Line 47th: 1 6 8
Line 48th: 2 6 8
Line 49th: 1 4 9
Line 50th: 2 4 9
Line 51st: 1 5 9
Line 52nd: 2 5 9
Line 53rd: 1 6 9
Line 54th: 2 6 9
Line 55th: 1 4 7
Line 56th: 2 4 7
Line 57th: 1 5 7
Line 58th: 2 5 7
Line 59th: 1 6 7
Line 60th: 2 6 7
Line 61st: 1 4 8
Line 62nd: 2 4 8
line 63rd: 1 5 8
Line 64th: 2 5 8
Line 65th: 1 6 8
Line 66th: 2 6 8
Line 67th: 1 4 9
Line 68th: 2 4 9
Line 69th: 1 5 9
Line 70th: 2 5 9
Line 71st: 1 6 9
Line 72nd: 2 6 9

Enter n arrays, array lengths, and each array takes a number to combine to find all the combinations.

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.