• 数组-13. 螺旋方阵


     1 /*
     2  * Main.c
     3  * E13-数组-13. 螺旋方阵
     4  *  Created on: 2014年8月25日
     5  *******测试通过***********
     6  *
     7  */
     8 
     9 #include <stdio.h>
    10 
    11 int main(void) {
    12 
    13     int array[11][11];
    14     int n; //题目中的N
    15 
    16     scanf("%d", &n);
    17 
    18     int upBound = 0; //上边界
    19     int bottomBound = n - 1; //下边界
    20     int leftBound = 0; //左边界
    21     int rightBound = n - 1; //右边界
    22     int num = 1; //矩阵的数值,从1开始
    23     int i;
    24 
    25     while (upBound <= bottomBound && leftBound <= rightBound) {
    26         for (i = leftBound; i <= rightBound; i++) {
    27             array[upBound][i] = num;
    28             num++;
    29         }
    30         if (upBound == bottomBound)
    31             break;
    32 
    33         upBound++; //上边界下移一行
    34         for (i = upBound; i <= bottomBound; i++) {
    35             array[i][rightBound] = num;
    36             num++;
    37         }
    38         if (leftBound == rightBound)
    39             break;
    40 
    41         rightBound--; //右边界左移一行
    42         for (i = rightBound; i >= leftBound; i--) {
    43             array[bottomBound][i] = num;
    44             num++;
    45         }
    46 
    47         bottomBound--; //下边界上移一行
    48         for (i = bottomBound; i >= upBound; i--) {
    49             array[i][leftBound] = num;
    50             num++;
    51         }
    52 
    53         leftBound++; //左边界左移一行
    54 
    55     }
    56     //输出矩阵
    57     int j;
    58     for (i = 0; i < n; i++) {
    59         for (j = 0; j < n; j++) {
    60             printf("%3d", array[i][j]);
    61         }
    62         printf("
    ");
    63     }
    64 
    65     return 0;
    66 }

    ***

    在这里声明一下,这道题目是我根据“YangKang”实现的C语言版本,但是因为我实在找不到网址了,所以下面我把“YangKang”的代码附上。如有知道具体网址的,还请告知于我,我会把出处添加上。

    ***

     1 /**
     2  * 问题描述:
     3  * 从键盘输入一个整数,
     4  * 则以该数字为矩阵的大小,把1,2,3,…,n*n 的数字按照顺时针螺旋的形式填入其中。
     5  * 例如:
     6  * 输入数字2,则程序输出:
     7  * 1 2
     8  * 4 3
     9  * 输入数字3,则程序输出:
    10  * 1 2 3
    11  * 8 9 4
    12  * 7 6 5
    13  * 输入数字4, 则程序输出:
    14  *  1  2   3  4
    15  * 12 13  14  5
    16  * 11 16  15  6
    17  * 10  9   8  7
    18  *
    19  * @author YangKang
    20  *
    21  */
    22 import java.util.Scanner;
    23 
    24 public class Main {
    25     public static void main(String[] args) {
    26         Scanner keyin = new Scanner(System.in);
    27 
    28         System.out.println("请输入矩阵大小");
    29         int n = keyin.nextInt();
    30         if (n == 0)
    31             System.exit(0);
    32         int result[][] = new int[n][n];// 存放最后的螺旋数字方阵
    33         /*
    34          * 程序中首先从上届为0的行开始将数据存入result中。达到右边界n-1时,上届指针下移一行,
    35          * 其他边界指针不动,此时行循环变量每次往下移动一行,列循环指针指向右边界不动,持续 插入递增的数字,
    36          */
    37         int upBound = 0;// 上届指针
    38         int downBound = n - 1;// 下界指针
    39         int leftBound = 0;// 左边界指针
    40         int rightBound = n - 1;// 右边界指针
    41         int num = 1;// 记录数字值,每插入一个,自增一次    
    42         while (upBound <= downBound && leftBound <= rightBound) {
    43             /*
    44              * 横向从左到右将数字从小到大插入数组对应位置。 j代表列循环变量
    45              */
    46             for (int j = leftBound; j <= rightBound; j++) {
    47                 result[upBound][j] = num;
    48                 num++;// 数字记录自增
    49             }
    50             if (upBound == downBound)// 避免重复插入行
    51                 break;
    52             /*
    53              * 一圈的最上一行插入完毕,开始向数组插入右侧列,方向为从上至下。 此时上届指针应该下移一行,其他指针不动,行循环变量逐层下移。
    54              * i代表行循环变量
    55              */
    56             upBound++;// 上届下移一行
    57             for (int i = upBound; i <= downBound; i++) {
    58                 result[i][rightBound] = num;
    59                 num++;
    60             }
    61             if (leftBound == rightBound)// 避免重复插入列
    62                 break;
    63             /*
    64              * 一圈的右边界列插入完毕,开始向下边界行插入数字,方向为从右向左。 此时右边界指针左移一行,其他指针不动,列循环变量逐列向做移动。
    65              * j代表列循环变量
    66              */
    67             rightBound--;// 右届左移一行
    68             for (int j = rightBound; j >= leftBound; j--) {
    69                 result[downBound][j] = num;
    70                 num++;
    71             }
    72             /*
    73              * 一圈的下边界行插入完毕,开始向左边界插入列数字,方向为从下往上。
    74              * 此时下边界指针向上移动一行,其他指针不动,行循环变量逐层向上移动。 i代表行循环变量
    75              */
    76             downBound--;// 下界上移一行
    77             for (int i = downBound; i >= upBound; i--) {
    78                 result[i][leftBound] = num;
    79                 num++;
    80             }
    81             /*
    82              * 至此一圈的插入过程完成,重现continue开始进入下一圈的插入过程。 此时左边界指针需要重新调整,即需向右移动一行。
    83              */
    84             leftBound++;// 左边界右移一行
    85         }
    86 
    87         /*
    88          * 输出方阵螺旋数组
    89          */
    90         for (int i = 0; i < n; i++) {
    91             for (int j = 0; j < n; j++) {
    92                 System.out.print(result[i][j] + "	");
    93             }
    94             System.out.println();
    95         }
    96     }
    97 }

    题目链接:

    http://pat.zju.edu.cn/contests/basic-programming/%E6%95%B0%E7%BB%84-13

    .

  • 相关阅读:
    向SDE库中写入栅格和矢量数据
    datagridview使用方法
    GP Statics
    GP tool , Resample, Mask, Clip
    好的影像库网站
    文件及文件夹相关操作
    使用GeoProcessing进行批处理的编程方式(粗粒度的编程)
    git clear local branches All In One
    node js module exports multiple variables All In One
    推荐的 iPad 绘画入门教程资源 All In One
  • 原文地址:https://www.cnblogs.com/boomkeeper/p/E13.html
Copyright © 2020-2023  润新知