Created
December 8, 2016 01:02
-
-
Save jianminchen/137807675ba27fd532ffaed4f98a68df to your computer and use it in GitHub Desktop.
HackerRank Matrix Rotation - Google - study code, C Programming, Julia likes the idea inside two for loop, for any element in the matrix, determine the position after rotations.
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
#include <stdio.h> | |
#include <string.h> | |
#include <math.h> | |
#include <stdlib.h> | |
int main() { | |
int rows,cols,rot; | |
scanf("%d%d%d",&rows,&cols,&rot); | |
int arr[rows][cols]; | |
int result[rows][cols]; | |
for (int r = 0; r < rows; r++) { | |
for (int c = 0; c < cols; c++) { | |
scanf("%d", &arr[r][c]); | |
} | |
} | |
rows--; | |
cols--; | |
for (int r = 0; r <= rows; r++) { | |
for (int c = 0; c <= cols; c++) { | |
int x = r < rows - r ? r : rows - r; | |
int y = c < cols - c ? c : cols - c; | |
int min = x < y ? x : y; | |
int maxRow = rows - min; | |
int maxCol = cols - min; | |
int parameter = (maxRow + maxCol) * 2 - min * 4; | |
int row = r; | |
int col = c; | |
for (int a = 0; a < rot%parameter; a++) { | |
if (col == min && row < maxRow) { | |
row++; | |
} | |
else if (row == maxRow && col < maxCol) { | |
col++; | |
} | |
else if (row > min && col == maxCol) { | |
row--; | |
} | |
else if (row == min && col > min) { | |
col--; | |
} | |
} | |
result[row][col] = arr[r][c]; | |
} | |
} | |
for (int r = 0; r <= rows; r++) { | |
for (int c = 0; c <= cols; c++) { | |
printf("%d ", result[r][c]); | |
} | |
printf("\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 31 - Line 45 - Julia likes the idea and implementation in C++.