Created
November 9, 2017 09:07
-
-
Save iUmarov/e2f1167d5aa1b2ff42f490798ef9aca3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package javaapplication1; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
/** | |
* | |
* @author Islom | |
*/ | |
public class JavaApplication1 { | |
// static List<List<Integer>> permutations = new ArrayList<List<Integer>>(); | |
static List<Integer> result = new ArrayList<>(); | |
// arguments are passed using the text field below this editor | |
public static void main(String[] args) | |
{ | |
ArrayList<Integer> first = init2(4); | |
ArrayList<Integer> second = init2(4); | |
// ArrayList<Integer> third = init2(3); | |
List<List<Integer>> lists = new ArrayList<>(); | |
lists.add(first); | |
lists.add(second); | |
// lists.add(third); | |
GeneratePermutations(lists, 0, 0); | |
System.out.println("End"); | |
} | |
static ArrayList<Integer> init(int n){ | |
ArrayList<Integer> res = new ArrayList<>(); | |
for(int i = 1; i <= n; i++){ | |
res.add(i); | |
} | |
return res; | |
} | |
static ArrayList<Integer> init2(int n){ | |
ArrayList<Integer> res = new ArrayList<>(); | |
for(int i = 0; i <= n; i++){ | |
res.add(i); | |
} | |
return res; | |
} | |
static void GeneratePermutations(List<List<Integer>> Lists, int depth, Integer sum) | |
{ | |
if(depth == Lists.size()) | |
{ | |
if(sum == 4){ | |
for(Integer cur: result){ | |
System.out.print(cur + " "); | |
} | |
System.out.print("sum: " + sum); | |
System.out.println(); | |
} | |
// result.clear(); | |
return; | |
} | |
for(int i = 0; i < Lists.get(depth).size(); ++i) | |
{ | |
if(result.size() > depth){ | |
result.set(depth, Lists.get(depth).get(i)); | |
} else { | |
result.add(depth, Lists.get(depth).get(i)); | |
} | |
GeneratePermutations(Lists, depth + 1, sum + Lists.get(depth).get(i)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment