Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company.Recursion.problems;
- import java.util.Scanner;
- public class Perms_NB {
- static int count;
- static int inputNum;
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- inputNum=input.nextInt();
- int[] set=new int[inputNum];
- int[] result=new int[inputNum];
- boolean[] used=new boolean[inputNum];
- fillSet(set);
- genPermutations(0,set,result,used);
- }
- private static void genPermutations(int index, int[] set,int[] result,boolean[]used){
- if(index==result.length){
- printPerm(result);
- }
- else{
- for (int i = 0; i < result.length; i++) {
- if (!used[i]) {
- used[i]=true;
- result[index] = set[i];
- genPermutations(index + 1, set, result,used);
- used[i]=false;
- }
- }
- }
- }
- private static void printPerm(int[] set) {
- //
- for (int i = 0; i < set.length - 1; i++) {
- System.out.print(set[i] + " ");
- }
- System.out.println(set[set.length - 1]);
- }
- private static int[] fillSet(int[] set) {
- for (int index = 0; index <set.length ; index++) {
- set[index]=index+1;
- }
- return set;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment