Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- import java.util.Scanner;
- public class permutations {
- public static void generatePermutations(int[] nums, int[] perm, boolean[] used, int n) {
- if (nums.length == n) {
- // perm now contains one permutation of nums.
- // Perform whatever operation you want. I printed here.
- //for (int k : perm)
- // System.out.print(k + " ");
- //System.out.println();
- } else {
- // Try shoving every unused number into perm at index n
- for (int i = 0; i < nums.length; i++) {
- if (used[i])
- continue;
- perm[n] = nums[i];
- used[i] = true;
- generatePermutations(nums, perm, used, n + 1);
- used[i] = false;
- }
- }
- }
- public static void permute(int[] nums)
- {
- generatePermutations(nums, new int[nums.length], new boolean[nums.length], 0);
- }
- public void run() {
- int[] arr = new int[10];
- for( int i = 0; i < arr.length; i++ )
- arr[i] = i;
- permute(arr);
- }
- public void debug(Object n) {
- System.out.println(n);
- }
- static Scanner in = new Scanner(System.in);
- static PrintWriter out = new PrintWriter(System.out);
- public static void main(String[] args) throws FileNotFoundException {
- // in = new Scanner(new File("permutations.in"));
- // out = new PrintWriter(new File("permutations.out"));
- permutations permutations = new permutations();
- permutations.run();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement