Created
December 28, 2016 22:37
-
-
Save jianminchen/1f74e6aa07831edabe98178dc9698fad to your computer and use it in GitHub Desktop.
Hackerrank week code 27 - hackonacci matrix rotation - study Java code
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
public class Solution { | |
private static final boolean[] hval = {true, true, false, true, false, false, true}; | |
private static boolean[][] matrix; | |
private static int count90; | |
private static int count180; | |
private static int count270; | |
private static boolean h(long n) { | |
int index = (int) (n % 7); | |
return hval[index]; | |
} | |
private static void buildMatrix(int n) { | |
matrix = new boolean[n][n]; | |
for (int i=1; i <= n; i++) { | |
for (int j=1; j <= n; j++) { | |
long val = (long)i * j; | |
val *= val; | |
matrix[i-1][j-1] = h(val); | |
} | |
} | |
} | |
private static void countDiffs() { | |
final int maxIndex = matrix.length - 1; | |
count90 = 0; | |
count180 = 0; | |
count270 = 0; | |
for (int i=0; i <= maxIndex; i++) { | |
for (int j=0; j <= maxIndex; j++) { | |
if (matrix[i][j] != matrix[j][maxIndex-i]) count90++; | |
if (matrix[i][j] != matrix[maxIndex-i][maxIndex-j]) count180++; | |
if (matrix[i][j] != matrix[maxIndex-j][i]) count270++; | |
} | |
} | |
} | |
private static int countDiffAfterRot(int angle) { | |
angle %= 360; | |
switch (angle) { | |
case 90: return count90; | |
case 180: return count180; | |
case 270: return count270; | |
} | |
return 0; | |
} | |
public static void main(String[] args) { | |
Scanner in = new Scanner(System.in); | |
int n = in.nextInt(); | |
buildMatrix(n); | |
countDiffs(); | |
int q = in.nextInt(); | |
for(int a0 = 0; a0 < q; a0++){ | |
int angle = in.nextInt() % 360; | |
// your code goes here | |
System.out.println(countDiffAfterRot(angle)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment