打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
螺旋数组,之字形数组
//螺旋数组

#include<iostream>using namespace std;//生成一个n*n维的螺旋数组,形式如下/*1 2 3 4 516 17 18 19 615 24 25 20 714 23 22 21 813 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  102  5  9  116  8  12 157  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;}

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
用C和Java分别实现杨辉三角
简说C语言数组指针的妙用
稀疏数组
c语言经典游戏代码
矩阵的最小路径和二维动态规划的空间压缩
数据结构-深度优先搜索(堆栈)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服