pat乙级真题 1050. 螺旋矩阵(25)

来源:互联网 发布:增加usb电流软件 编辑:程序博客网 时间:2024/05/20 18:46

题目链接

                   点击打开链接


注意事项

1 不能设置二维数组来存储矩形,要用一维数组来存储。 2 把每层赋值到矩阵时,要注意该行或该列是否还需要赋值。如7*1的矩阵,赋值了右边列时,左边列就不能赋值了,因为只有一列。         

代码

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>void quick_sort(int a[],int first,int end){int i, j,temp = a[first];i = first;j = end;if (i<j){while (i < j){while (i < j && a[j] >= temp) j--;a[i] = a[j];while (i<j && a[i]<=temp) i++;a[j] = a[i];}a[i] = temp;quick_sort(a,first,i-1);quick_sort(a, i+1, end);}}int main(){int N, i, j, n, m;int  juzheng[10002],input[10100];scanf("%d", &N);for (i = 0; i < N; i++){scanf("%d", &input[i]);}quick_sort(input, 0, N - 1);for (i = 1; i <= sqrt((double)N); i++){//找出满足m*n == N 且m -n 最小if (N%i == 0){m = N / i;n = i;}}int  count, input_num;input_num = N - 1;  //我的排序为递增,所以最后一个元素为最大。count = 0;         //标志层数while (2 * count < m || 2 * count < n){ //注意输出改行或该列前,是否还需要输出。上边行和右边列需要满足m - 2 * count >0,才需要输出;下边行和左边列需要m - 2 * count >1才需要输出for (i = count; i < n - count && m - 2 * count >0; i++){ //一层中的上方的行juzheng[count* n +i] = input[input_num--];}for (i = count + 1; i < m - count && n - 2 * count >0; i++){//一层中右边的列juzheng[i * n + n - 1 - count] = input[input_num--];}for (i = n - count - 2; i >= count && m - 2 * count >1; i--){//一层中下方的行juzheng[(m - 1 -count)* n + i] = input[input_num--];}for (i = m - 2 - count; i > count && n - 2 * count >1;i--){//一层中左边的列。juzheng[i * n +count] = input[input_num--];}count++; //层数+1}for (i = 0; i < m;i++){ //输出矩阵for (j = 0; j < n;j++){if (j == n-1)printf("%d\n", juzheng[i*n +j]);elseprintf("%d ", juzheng[i*n+j]);}}return 0;}


原创粉丝点击