螺旋数组,之字形数组
//螺旋数组
#include<iostream> using namespace std; //生成一个n*n维的螺旋数组,形式如下 /* 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9 */ void spiral_array(int **a,int n) { /*for (int i = 0; i < n; i++) { for(int j = 0; j < n; j++) printf("%d ",a[i][j]); printf("\n"); }*/ int terminated = n/2; int m = 1; for (int i = 0; i < terminated; i++) { for (int j = 0; j < n-i; j++) { if (a[i][j] == 0) { a[i][j] = m++; } } for (int j = i+1; j<n-i; j++) { if (a[j][n-i-1]==0) { a[j][n-i-1]=m++; } } for (int j = n-i-1; j > i; j--) { if (a[n-i-1][j]==0) { a[n-i-1][j]=m++; } } for (int j = n-i-1; j>i; j--) { if (a[j][i]==0) { a[j][i]=m++; } } if (n%2==1) { a[terminated][terminated]=m; } } } int main() { int **a; int n; printf("请输入数组的维数:"); scanf("%d",&n); //二维数组动态内存分配 a = (int **)malloc(sizeof(int*)*n); for (int i = 0; i < n; i++) { a[i] = (int *)malloc(sizeof(int)*n); for(int j = 0; j < n; j++) a[i][j] = 0; } spiral_array(a,n); printf("螺旋数组:\n"); for (int i = 0; i < n; i++) { for(int j = 0; j < n; j++) printf("%d ",a[i][j]); printf("\n"); } //释放内存 for (int i = 0; i < n; i++) { free(a[i]); } free(a); return 0; }
之字形数组
/* 实现一个之字形数组 ,从1 开始,之字形形成数组 1 3 4 10 2 5 9 11 6 8 12 15 7 13 14 16 */ #include<stdio.h> #include <stdlib.h> //using namespace std; void constructArray(int **array,int N) { //说明,方向坐标,每两个单位表示一个反向, //一共四个方向,比如 //direction[0]、direction[1]表示第一个方向的x 和 y的走向 //direction[2]、direction[3]表示第二个方向的x 和 y的走向 //direction[4]、direction[5]表示第三个方向的x 和 y的走向 //direction[6]、direction[7]表示第四个方向的x 和 y的走向 int direction[8]={1,0,-1,1,0,1,1,-1}; int row = 0, col = 0; bool changeDirec = false; int index = 1; array[row][col] = index; while(1) { for(int i = 0; i < 4; i++) { if (row == N-1 && col == N-1)//最后一个位置跳出 break; if(row == N-1 && N%2==1 && changeDirec==false)//奇数行换方向 { direction[0]=0; direction[1]=1; direction[2]=-1; direction[3]=1; direction[4]=1; direction[5]=0; direction[6]=1; direction[7]=-1; changeDirec = true; break; } if(col == N-1 && N%2==0 && changeDirec==false)//偶数行换方向 { direction[0]=1; direction[1]=0; direction[2]=1; direction[3]=-1; direction[4]=0; direction[5]=1; direction[6]=-1; direction[7]=1; changeDirec = true; break; } if (changeDirec==false) { if (i%2==0) { row += direction[2*i]; col += direction[2*i+1]; array[row][col] = ++index; } else { while(1) { if ((direction[2*i]+row)<0 || (col+direction[2*i+1])<0 || (direction[2*i]+row)>=N || (col+direction[2*i+1])>=N)break;//越界判断 row += direction[2*i]; col += direction[2*i+1]; array[row][col] = ++index; } } }else { if (i%2==0) { row += direction[2*i]; col += direction[2*i+1]; array[row][col] = ++index; } else { while(1) { if ((direction[2*i]+row)<0 || (col+direction[2*i+1])<0 || (direction[2*i]+row)>=N || (col+direction[2*i+1])>=N)break;//越界判断 row += direction[2*i]; col += direction[2*i+1]; array[row][col] = ++index; } } } } if (row == N-1 && col == N-1) break; } } int main() { int **array; int N; printf("请输入数组N*N的维数N:\n"); scanf("%d",&N); //内存分配 array = (int **)malloc(N*sizeof(int *)); if (array == NULL) { return -1; } for (int i = 0; i < N; i++) { array[i] = (int *)malloc(N*sizeof(int)); if(array[i]==NULL)return -1; } //初始化 for(int i = 0; i < N; i++) for(int j = 0; j < N; j++) { array[i][j] = 0; } constructArray(array,N); for(int i = 0; i < N; i++) { for(int j = 0; j < N; j++) { printf("%d ",array[i][j]); } printf("\n"); } //内存释放 for (int i = 0; i < N; i++) { free(array[i]); } free(array); return 0; }
posted on 2014-08-12 11:16 NewPanderKing 阅读(892) 评论(0) 编辑 收藏 举报