最近面试某互联网公司碰到一道编程题,要求写一个蛇形矩阵的生成函数以及索引函数,感觉挺有意思的,实现代码如下
1. 矩阵生成函数:
void SnakeMatrix(int** matrix, const int dim, const int direction) {
if (dim <= 0) {
std::cout << "Invalid dim!" << std::endl;
return;
}
if (direction != 0 && direction != 1) {
std::cout << "Invalid direction!" << std::endl;
}
int x = 0;
int y = 0;
int value = 1;
int head = direction;
matrix[y][x] = value;
if (dim == 1) {
std::cout << value << std::endl;
return;
}
if (direction == 0) {
x++;
} else {
y++;
}
matrix[y][x] = ++value;
while (x < dim && y < dim) {
if (x == dim - 1 && y == dim - 1){
break;
}
if (head == 0) {
if ( x > 0 && y < dim - 1) {
x--;
y++;
matrix[y][x] = ++value;
} else {
head = 1;
if (y < dim - 1){
y++;
} else {
x++;
}
matrix[y][x] = ++value;
}
} else {
if ( y > 0 && x < dim - 1) {
x++;
y--;
matrix[y][x] = ++value;
} else {
head = 0;
if (x < dim - 1) {
x++;
} else {
y++;
}
matrix[y][x] = ++value;
}
}
}
std::cout << "Snake Shape Matrix: " << std::endl;
for (int i = 0; i < dim; ++i) {
for (int j = 0; j < dim; ++j) {
std::cout << matrix[i][j] << " ";
}
std::cout << std::endl;
}
}
2. 索引函数:
void Find(const int dim, const int value, const int start, int& x, int& y) {
int left_value = value;
if (left_value <= 0 || left_value>dim * dim) {
std::cout << left_value << " is not in matrix" << std::endl;
return;
}
for (int i = 1; i <=dim; ++i) {
if ( left_value > i) {
left_value -= i;
}
else {
if (start == 0) {
if (i % 2 == 1){
left_value--;
x = left_value;
y = i - left_value - 1;
return;
}
else {
left_value--;
x = i - left_value - 1;
y = left_value;
return;
}
}
else {
if (i % 2 == 0){
left_value--;
x = left_value;
y = i - left_value - 1;
return;
}
else {
left_value--;
x = i - left_value - 1;
y = left_value;
return;
}
}
}
}
for (int i = dim - 1; i >= 1; --i) {
if (left_value > i) {
left_value -= i;
}
else {
if (start == 0) {
if (i % 2 == 1){
left_value--;
x = dim - i +left_value;
y = dim - left_value - 1;
return;
}
else {
left_value--;
x = dim - left_value - 1;
y = dim - i + left_value;
return;
}
}
else {
if (i % 2 == 0){
left_value--;
x = dim - i + left_value;
y = dim - left_value - 1;
return;
}
else {
left_value--;
x = dim - left_value - 1;
y = dim - i + left_value;
return;
}
}
}
}
}
3.测试主函数
int main(int argc char *argv[]){
int dim;
std::cout << "Input matrix dimension: " << std::endl;
std::cin >> dim;
while (dim <= 0) {
std::cout << "Matrix dimension must be positive, please input again: " << std::endl;
std::cin >> dim;
}
int** matrix = new int*[dim];
for (int i = 0; i < dim; ++i) {
matrix[i] = new int[dim];
}
char direction;
std::cout << "Input direction, 'r' or ' d' : " << std::endl;
std::cin >> direction;
while (direction != 'r' && direction != 'd') {
std::cout << "Invalid direction, please input again('r' or 'd') : " << std::endl;
std::cin >> direction;
}
int start = direction == 'r' ? 0 : 1;
SnakeMatrix(matrix, dim, start);
int x, y;
int value;
std::cout << "Input search value: " << std::endl;
std::cin >> value;
Find(dim, value, start, x, y);
std::cout << value << " in row: " << y << " column: " << x << std::endl;
for (int i = 0; i < dim; ++i) {
delete[] matrix[i];
}
delete[] matrix;
getchar();
return 0;
}