fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone {
  9.  
  10. public static void main (String[] args) throws java.lang.Exception {
  11. p(8);
  12. p(9);
  13. }
  14.  
  15. private static void p(int n) {
  16. System.out.println("Input: " + n);
  17. System.out.print("Output:");
  18. var start = (int) Math.ceil(n/2.0);
  19. var even = (n & 1) == 0;
  20. tailrec(start, even, start, -1);
  21. System.out.println();
  22. }
  23.  
  24. private static void tailrec(int start, boolean even, int current, int step) {
  25. if (current > start) {
  26. return;
  27. }
  28. System.out.print(" " + current);
  29. if (current > 1) {
  30. tailrec(start, even, current+step, step);
  31. } else {
  32. if (even) { System.out.print(" " + current); }
  33. tailrec(start, even, current+1, 1);
  34. }
  35. }
  36. }
Success #stdin #stdout 0.09s 48276KB
stdin
Standard input is empty
stdout
Input: 8
Output: 4 3 2 1 1 2 3 4
Input: 9
Output: 5 4 3 2 1 2 3 4 5