码迷,mamicode.com
首页 > 其他好文 > 详细

蛇形矩阵 和螺旋矩阵

时间:2014-09-11 19:30:22      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:io   ar   for   sp   on   c   amp   size   ef   

1.蛇形方阵:

          1  2  3

          6  5  4

          7  8  9

外层控制层数,内层根据列数控制方向:0 则往右走   N-1则往左走

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 100
int a[SIZE][SIZE];

int main(int argc, const char *argv[])
{
    int dim = atoi(argv[1]);
    int i, j, k, lev;
    int num =0;
    j = 0;
    for(lev = 0; lev < dim; lev++)
    {
        if(j == 0)
        {
            while(j < dim)
                a[lev][j++] = ++num;
            j--;
            a[lev+1][j] = num;
        }
        else if(j == dim-1)
        {
          while(j >= 0)   
              a[lev][j--] = ++num;
          j++;
         a[lev+1][j] = num;  
        }
    }

    for(i = 0; i < dim; ++i)
    {
     for(j = 0; j < dim; ++j)
             printf("%-4d", a[i][j]);
      printf("\n");
    }
    return 0;
}


2.螺旋方阵

     1      2      3       4

     12   13     14     5 

     11   16     15     6

     10    9     8        7

方法:根据数字或者圈数来控制外层循环,内层循环还是控制四个方向

根据数字:   

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 100
int a[SIZE][SIZE];//全局变量,数组未初始化,元素为0

int main(int argc, const char *argv[])
{
    int dim = atoi(argv[1]);
    int x, y, count;
    x = y = count = 0;
    a[0][0] = 1;
    count++;
    while(count < dim * dim)
    {
        while(y < dim - 1 && a[x][y+1] == 0 )
            a[x][++y] = ++count;
        while(x < dim - 1 && a[x+1][y] == 0)
            a[++x][y] = ++count;
        while(y > 0 && a[x][y-1] == 0)
            a[x][--y] = ++count;
        while(x > 0 && a[x-1][y] == 0)
            a[--x][y] = ++count;
    }
    for(x = 0; x < dim; ++x)
    {
        for(y = 0; y < dim; ++y)
            printf("%-4d", a[x][y]);
        printf("\n");
    }
    return 0;
}


根据圈数:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 100
int a[SIZE][SIZE];

int main(int argc, const char *argv[])
{
    int dim = atoi(argv[1]);
    int i,j;
    int index_x;
    int left_up, right_down;
    int circle = dim % 2 ? dim / 2 +1 : dim / 2;
    int num = dim*dim +1;
    for(index_x = 0; index_x < circle; index_x++)
    {
        left_up = index_x;//左上角
        right_down = dim - left_up -1;//右下角
        for(i = left_up, j = left_up; j < right_down; ++j)
            a[i][j] = --num;
        for(i = left_up, j = right_down; i < right_down; ++i)
            a[i][j] = --num;
        for(i = right_down, j = right_down; j > left_up; --j)
            a[i][j] = --num;
        for(i = right_down, j = left_up; i > left_up; --i)
            a[i][j] = --num;
    }
    for(i = 0; i < dim; ++i)
    {
        for(j = 0; j < dim; ++j)
            printf("%-4d", a[i][j]);
        printf("\n");
    }

    return 0;
}




蛇形矩阵 和螺旋矩阵

标签:io   ar   for   sp   on   c   amp   size   ef   

原文地址:http://blog.csdn.net/aa838260772/article/details/39208351

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!