bobby_k

perms

Jun 8th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.37 KB | None | 0 0
  1. package com.company.Recursion.problems;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Perms_NB {
  6.     static int count;
  7.     static int inputNum;
  8.     public static void main(String[] args) {
  9.         Scanner input = new Scanner(System.in);
  10.         inputNum=input.nextInt();
  11.         int[] set=new int[inputNum];
  12.         int[] result=new int[inputNum];
  13.         boolean[] used=new boolean[inputNum];
  14.         fillSet(set);
  15.         genPermutations(0,set,result,used);
  16.  
  17.     }
  18.  
  19.     private static void genPermutations(int index, int[] set,int[] result,boolean[]used){
  20.  
  21.         if(index==result.length){
  22.                 printPerm(result);
  23.         }
  24.         else{
  25.  
  26.             for (int i = 0; i < result.length; i++) {
  27.                 if (!used[i]) {
  28.                     used[i]=true;
  29.                     result[index] = set[i];
  30.                     genPermutations(index + 1, set, result,used);
  31.                     used[i]=false;
  32.                 }
  33.             }
  34.         }
  35.     }
  36.  
  37.     private static void printPerm(int[] set) {
  38.         //
  39.         for (int i = 0; i < set.length - 1; i++) {
  40.             System.out.print(set[i] + " ");
  41.         }
  42.         System.out.println(set[set.length - 1]);
  43.     }
  44.     private static int[] fillSet(int[] set) {
  45.         for (int index = 0; index <set.length ; index++) {
  46.             set[index]=index+1;
  47.         }
  48.         return set;
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment