Advertisement
Guest User

Just do it ;-;

a guest
Oct 20th, 2015
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.98 KB | None | 0 0
  1. package com.stackOverflow.questions;
  2.  
  3. public class PrintArrays
  4. {
  5.     static int X = 5;  // for example
  6.     static int N = 7; // for example
  7.    
  8.     public static void main(String[] args)
  9.     {
  10.     //get X and N from the user.
  11.    
  12.     int arr[] = new int[N];
  13.    
  14.     doYourThing(arr, 0, 0);
  15.     }
  16.    
  17.     public static void doYourThing(int arr[], int num, int mode) //mode = is he need to count up or stop when he get to X
  18.     {
  19.     if(num == N)
  20.     {
  21.         return;
  22.     }
  23.     int alreadyHas = (num != 0 && mode != 1)?(1):(0);
  24.     for(int i = alreadyHas; i < X; i++)
  25.     {
  26.         arr[num] = i;
  27.         printArr(arr);
  28.         if(num > 0)
  29.         {
  30.         doYourThing(arr, num-1, 1);
  31.         }
  32.     }
  33.     arr[num] = 0;
  34.     if(mode == 0)
  35.     {
  36.         doYourThing(arr, num+1, 0);
  37.     }
  38.     }
  39.    
  40.     public static void printArr(int arr[])
  41.     {
  42.     String str;
  43.     str = "[";
  44.     for(int i = arr.length-1; i >= 0; i--)
  45.     {
  46.         str += arr[i] + ",";
  47.     }
  48.     str = str.substring(0, str.length()-1);
  49.     str += "]";
  50.     System.out.println(str);
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement