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. {
  12. // your code goes here
  13. Scanner sc = new Scanner(System.in);
  14. int t = sc.nextInt(), curr = 1;
  15. while(t-->0){
  16. int n = sc.nextInt();
  17. char [][] res = new char [n][n];
  18. int top = 0 , left = 0,bottom = n-1,right = n-1;
  19. System.out.println("Case #"+curr+":");
  20. while(left<= right && top <= bottom){
  21. for(int i=left; i<= right;i++)
  22. res[top][i] = '*';
  23. top++;
  24. if(left != 0)
  25. left++;
  26. for(int i=top; i<= bottom;i++)
  27. res[i][right] = '*';
  28. right--;
  29. if(top < bottom){
  30. for(int i=right; i>= left;i--)
  31. res[bottom][i] = '*';
  32. bottom--;
  33. }
  34. top++;
  35. if(left <= right){
  36. for(int i= bottom; i >= top;i--)
  37. res[i][left] = '*';
  38. left++;
  39. }
  40. right--;
  41. bottom--;
  42. }
  43. for(int i=0;i<n;i++){
  44. for(int j=0;j<n;j++){
  45. System.out.print((res[i][j]=='*')?'*'+" ":" ");
  46. }
  47. System.out.println();
  48. }
  49. curr++;
  50. }
  51. }
  52. }
Success #stdin #stdout 0.18s 60852KB
stdin
2
5
10
stdout
Case #1:
* * * * * 
        * 
* * *   * 
*       * 
* * * * * 
Case #2:
* * * * * * * * * * 
                  * 
* * * * * * * *   * 
*             *   * 
*   * * * *   *   * 
*   *     *   *   * 
*   *         *   * 
*   * * * * * *   * 
*                 * 
* * * * * * * * * *