Viksy

Variations

Sep 14th, 2022
733
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.72 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Variations {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         int length = Integer.parseInt(scanner.nextLine());
  8.  
  9.         String[] pair = scanner.nextLine().split("\\s+");
  10.  
  11.         printAllVariations(pair, "", length);
  12.     }
  13.  
  14.     private static void printAllVariations(String[] pair, String currVariation, int length) {
  15.         if(length == 0) {
  16.             System.out.println(currVariation);
  17.             return;
  18.         }
  19.  
  20.         for (int i = 0; i < pair.length; i++) {
  21.             String newVariation = currVariation + pair[i];
  22.  
  23.             printAllVariations(pair, newVariation, length - 1);
  24.         }
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment