// Arup Guha
// 10/6/05
// Solution to COP 3530 Recitation #5 Problem B: Tiling.

import java.io.*;

public class tile {

  public static void main(String[] args) throws IOException {

    BufferedReader stdin = new BufferedReader
				(new InputStreamReader(System.in));

    System.out.println("Would you like to print tile combinations?(y/n)");
    char c = (stdin.readLine()).charAt(0);

    // Loop until user wants to quit.
    while (c == 'y' || c == 'Y') {

      // Get the user input.
      System.out.println("What is the length of the row of tiles?");
      int n = Integer.parseInt(stdin.readLine());

      // Don't try invalid cases.
      if (n < 1) {
        System.out.println("Sorry that is invalid.\n");
        continue;
      }

      // Print out the output.
      System.out.println("Here is your output:");
      print(n);
      System.out.println();

      // See if they want to try this again.
      System.out.println("Would you like to print tile combinations again?(y/n)");
      c = (stdin.readLine()).charAt(0);

    }

  }

  // Wrapper function for the recursive function that does the printing.
  public static void print(int n) {

    // We start the buffer with nothing, needing need the rest of the
    // values to sum to n.
    printAll("", n);
  }

  // The buffer stores the tiles that are already set, and n represents
  // the length of the remaining tile to separate out. This method will
  // print out all of the orderings of the tile that start with the 
  // buffer and then add up to length n after that.
  public static void printAll(String buffer, int n) {


    // We can only add a tile of length 1 to the buffer.
    if (n == 1) {
      System.out.println(buffer+" 1");
      return;
    }

    // There are two ways to tile the last 2 squares, either one tile of
    // length 2 or two tiles of length 1.
    if (n == 2) {
      System.out.println(buffer+" 2");
      System.out.println(buffer+" 1, 1");
      return;
    }

    // Otherwise, we add a tile of size 2 to the buffer, and then 
    // recursively tile the remaining length of n-2, OR, we can add
    // a tile of size 1 to the buffer, and then recursively tile the
    // remaining length of n-1.
    printAll(buffer+" 2,", n-2);
    printAll(buffer+" 1,", n-1);
    
  }

}
