打印螺旋矩阵
2012-07-25 21:57
253 查看
see: http://www.geeksforgeeks.org/archives/10768
August 20, 2011
Given a 2D array, print it in spiral form. See the following examples.
Answer:
Print a given matrix in spiral form
August 20, 2011Given a 2D array, print it in spiral form. See the following examples.
Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Output: 1 2 3 4 5 6 12 18 17 16 15 14 13 7 8 9 10 11
Answer:
/* Paste your code here (You may delete these lines if not writing code) */ void spiralmatrix(int arr[], int m, int n) { int rowstart=0, rowend=m-1, colstart=0, colend=n-1; int i,j; while(rowstart<=rowend&&colstart<=colend) { int i=rowstart, j=colstart; for(j=colstart;j<=colend;j++) { printf("%d",arr[i][j]); } for(i=rowstart+1,j--;i<=rowend;i++) { printf("%d",arr[i][j]); } for(j=colend-1,i--;j>=colstart;j--) { printf("%d",arr[i][j]); } for(i=rowend-1,j++;i>=rowstart+1;i--) { printf("%d",arr[i][j]); } rowstart++, rowend--, colstart++,colend--; } }
相关文章推荐
- [算法]方正面试题:N×N矩阵螺旋打印输出
- [数组]顺时针打印螺旋矩阵
- 打印螺旋举矩阵-java
- N×N矩阵螺旋打印输出
- 打印螺旋矩阵
- 1,2,3…n*n 的数字按照顺时针螺旋的形式打印成矩阵(递归)
- 打印螺旋矩阵
- 螺旋矩阵2——顺时针打印任意大小的矩阵
- 【编程习题★★☆☆☆】螺旋打印N*N的矩阵
- 打印螺旋矩阵
- 打印螺旋数字矩阵
- C/C++面试之算法系列--打印 N*N 螺旋矩阵
- Python-打印螺旋矩阵
- 螺旋矩阵顺时针打印
- 打印螺旋数字矩阵
- 打印n*n矩阵的螺旋方形和回字形
- N×N矩阵螺旋打印输出
- 简单螺旋打印矩阵 - 直接, 好理解
- 飘逸的python - 打印螺旋矩阵
- 顺时针打印矩阵或者 螺旋数组