Skip to content

Instantly share code, notes, and snippets.

@diogoalexsmachado
Created March 27, 2018 23:07
Show Gist options
  • Save diogoalexsmachado/e45549fc66cad2574d600664c3741a24 to your computer and use it in GitHub Desktop.
Save diogoalexsmachado/e45549fc66cad2574d600664c3741a24 to your computer and use it in GitHub Desktop.
package com.teste.app;
public class Main {
public static void main(String[] args) {
int[][] nums = {{ 1, 2, 3, 4},
{ 5, 6, 7, 8},
{ 9, 10, 11, 12},
{13, 14, 15, 16}};
int[][] nums2 = {{ 1, 2, 3},
{ 4, 5, 6},
{ 7, 8, 9},
{ 10, 11, 12}};
int[][] nums3 = {{ 1, 2, 3, 5, 6},
{ 15, 16, 17, 18, 7},
{ 14, 21, 20, 19, 8},
{ 13, 12, 11, 10, 9}};
System.out.println("nums 1 : ");
printStuff(nums);
System.out.println("\nnums 2 : ");
printStuff(nums2);
System.out.println("\nnums 3 : ");
printStuff(nums3);
}
public static void printStuff(int[][] nums){
//tamanho da matriz (dinâmico)
int m = nums.length;
int n = nums[0].length;
int i, row = 0, column = 0;
while (row < m && column < n) {
// Print the top row
for (i = column; i < n; ++i)
System.out.print(nums[row][i]+" ");
row++;
// Print the right column
for (i = row; i < m; ++i)
System.out.print(nums[i][n-1]+" ");
n--;
// Print the bottom row
if ( row < m) {
for (i = n-1; i >= column; --i)
System.out.print(nums[m-1][i]+" ");
m--;
}
// Print the left column
if (column < n) {
for (i = m-1; i >= row; --i)
System.out.print(nums[i][column]+" ");
column++;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment