Pergunta de entrevista da empresa Meta

Given a matrix print it clockwise from the first element to the very inner element.

Respostas da entrevista

Sigiloso

10 de nov. de 2010

void PrintMatrix(int** a, int n) { for (int i = 0; i = i; j--) { printf("%d ", a[n - i -1][j]); } } void PrintColInc(int** a, int n, int i) { for (int j = i; j = i; j--) { printf("%d ", a[j][i]); } }

1

Sigiloso

31 de jan. de 2011

struct point { point(int a, int b) { row = a; col = b; } int row; int col; }; void printMatrixSpiral(int a[][5], int r, int c) { int direction = 1; // 1 - right // 2 - down // 3 - left // 4 - up point upper_left = point(0,0); //point upper_right = point(0,c-1); point lower_right = point(r-1, c-1); //point lower_bottom = point(r-1, 0); while(true){ if(direction==1){ int row = upper_left.row; for(int i=upper_left.col; i= upper_left.col;--i){ cout= upper_left.row;--i){ cout lower_right.row) || (upper_left.col > lower_right.col)){break;} } }

Sigiloso

19 de ago. de 2011

int matrix[][3] = { {1, 2, 3}, { 4, 5, 6}, {7, 8, 9} }; void PrintMatrix() { int start = 0; int end = 2; while(end >= start) { for(int col = start; col = start; col--) cout start; row--) cout << matrix[row][start] << " "; start++; end--; } }

Sigiloso

14 de jan. de 2013

^ Above anon, your solution is almost correct. But for the 3rd and 4th for loops you first need to check whether you've already printed the corresponding previous for loop. Ok I didn't explain that too clearly. Here's an example: Matrix: {{1,2,3,4},{5,6,7,8},{9,10,11,12}} will print 1 2 3 4 8 12 11 10 9 5 6 7 6 That last 6 is because of the 3rd for-loop. You don't need to run that opposite for-loop (R-L) once you've already printed 6 from L-R. Just use an if-loop to rectify this. Same for the 4th loop.

Sigiloso

5 de fev. de 2012

int main() { int m, n; cin >> m >> n; int matrix[m][n]; for (int i = 0; i > matrix[i][j]; int l = 0, r = n, t = 0, b = m; while (l = l; i--) cout = t; i--) cout << matrix[i][l] << ' '; l++; } return 0; }

Sigiloso

1 de out. de 2010

Need to design the program first and then implement it very carefully yet fast!

1