Skip to content

Instantly share code, notes, and snippets.

@goctave
Created April 21, 2012 06:56
Show Gist options
  • Save goctave/2434984 to your computer and use it in GitHub Desktop.
Save goctave/2434984 to your computer and use it in GitHub Desktop.
CareerCup_1.6@1point3acres
/*
Q: Given an image represented by an NxN matrix,
where each pixel in the image is 4 bytes, write
a method to rotate the image by 90 degrees
Can you do this in place?
*/
/*
C++传递二维数组比较麻烦
*/
#include <iostream>
using namespace std;
void rotate(int matrix[][4], int n)
{
for(int i = 0; i < n/2; i++)
{
for(int j = 0; j < n/2; j++)
{
int temp = matrix[i][j];
matrix[i][j] = matrix[j][n - i - 1];
matrix[j][n - i - 1] = matrix[n - i - 1][n - j - 1];
matrix[n - i - 1][n - j - 1] = matrix[n - j - 1][i];
matrix[n - j - 1][i] = temp;
}
}
}
int main()
{
int matrix[4][4] = {1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16
};
rotate(matrix, 4);
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
cout << matrix[i][j] << " ";
cout << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment