Created
March 6, 2017 16:22
-
-
Save jaecktec/64537a0a92c64b2b3f380a743b3d7a08 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package testproj.uebung; | |
public class Problem6 { | |
private static final int[][] INPUT = { | |
{ 1, 2, 3, 4 }, | |
{ 5, 6, 7, 8 }, | |
{ 9, 10, 11, 12 }, | |
{ 13, 14, 15, 16 } | |
}; | |
public static void main(String[] args) { | |
print(INPUT); | |
// iterate though top | |
for (int ring = 0; ring < Math.floor(INPUT.length / 2); ring++) { | |
// shift north | |
int workRowStart = ring; | |
int workRowEnd; | |
int workColStart = ring; | |
int workColEnd = INPUT.length - ring; | |
int temp = INPUT[workRowStart][workColStart]; | |
for (int col = workColStart; col < workColEnd; col++) { | |
int original = INPUT[workRowStart][col]; | |
INPUT[workRowStart][col] = temp; | |
temp = original; | |
} | |
// shift east | |
workRowStart = ring + 1; | |
workRowEnd = INPUT.length - ring; | |
workColStart = INPUT[ring].length - ring - 1; | |
for (int row = workRowStart; row < workRowEnd; row++) { | |
int original = INPUT[row][workColStart]; | |
INPUT[row][workColStart] = temp; | |
temp = original; | |
} | |
// shift south | |
workRowStart = INPUT.length - ring - 1; | |
workColStart = INPUT[ring].length - ring - 2; | |
workColEnd = ring; | |
// we need to move from right to left here | |
for (int col = workColStart; col >= workColEnd; col--) { | |
int original = INPUT[workRowStart][col]; | |
INPUT[workRowStart][col] = temp; | |
temp = original; | |
} | |
// shift west | |
// we need to shift from bot to top | |
workRowEnd = ring; | |
workRowStart = INPUT.length - ring - 1; | |
workColStart = ring; | |
for (int row = workRowStart; row >= workRowEnd; row--) { | |
int original = INPUT[row][workColStart]; | |
INPUT[row][workColStart] = temp; | |
temp = original; | |
} | |
} | |
print(INPUT); | |
} | |
public static void print(int[][] input) { | |
System.out.println("---------------------------------"); | |
for (int idxa = 0; idxa < input.length; idxa++) { | |
System.out.print("|"); | |
for (int idxb = 0; idxb < input[idxa].length; idxb++) { | |
System.out.printf("%d \t| ", input[idxa][idxb]); | |
} | |
System.out.println(""); | |
} | |
System.out.println("---------------------------------"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment