二维数组蛇形和Z形矩阵输出
来源:互联网 发布:青岛灭门案犯落网知乎 编辑:程序博客网 时间:2024/06/07 07:53
1、蛇形矩阵输出
思路:就顺时针的矩阵为例,由矩阵外至内圈的数值是逐渐加1的,所以主要是找出值循环增加的规律,跟设定的圈数round有关,而round的取值范围为0 <= round <= n / 2,首先定义圈数从0开始,→的下标x是恒定的,从0开始,但是下标y是从round至n - round的,接着向下↓,下标y保持n - round 不变,下标x从round + 1增加到n - round - 1,;再接着向左←,下标x保持n - round - 1不变,而下标y则由n - round - 1减到round,最后向上↑,保持下标y为round不变,再将下标x由n - round - 1减少到round + 1,一个圈结束后,round加1,继续循环,最后判断一下输入的矩阵函数是奇数还是偶数,如果是奇数就不能忘了矩阵最中间的数,最后输出的数。矩阵的各个数都已经设定好了,就以依次打印出矩阵。
#include <stdio.h>#include <stdlib.h>int main(){ int x; /*二维数组的第一个下标*/ int y; /*二维数组的第二个下标*/ int n; /*创建的矩阵为n*n*/ int count = 1; /*蛇形矩阵由1开始计数*/ int round; /*矩阵转的圈数*/ printf("Please input the size of the array you want to be(n * n):\n"); scanf("%d",&n); int (*a)[n] = calloc(n * n,sizeof(int)); //如果n = 1,则直接输出 if(n == 1) { a[0][0] = count; } else { for(round = 0;round <= n / 2;round++) { x = round; for(y = round;y < n - round;y++) { a[x][y] = count; count++; } y = n - round - 1; for(x = round + 1;x < n - round - 1;x++) { a[x][y] = count; count++; } x = n - round - 1; for(y = n - round - 1;y >= round;y--) { a[x][y] = count; count++; } y = round; for(x = n - round - 2;x > round;x--) { a[x][y] = count; count++; } } if(n % 2 == 1) { a[n / 2][n / 2] = count; } } for(x = 0;x< n;x++) { for(y = 0;y < n;y++) { printf("%3d",a[x][y]); } printf("\n"); } printf("\n"); return 0;}调试结果:
Please input the size of the array you want to be(n * n):5 1 2 3 4 5 16 17 18 19 6 15 24 27 20 7 14 23 22 21 8 13 12 11 10 9
#include <stdio.h>#include <stdlib.h>int main(){ int n; int x; int y; int round; int count = 1; printf("Please input the size of array:\n"); scanf("%d",&n); int (*a)[n] = calloc(n * n,sizeof(int)); if(n == 1) { a[0][0] = count; } else { for(round = 0;round < n / 2;round++) { y = round; for(x = round;x < n - 1 - round;x++) { a[x][y] = count; count++; } x = n - 1 - round; for(y = round;y < n - 1 -round;y++) { a[x][y] = count; count++; } y = n - 1 - round; for(x = n - 1 - round;x > round;x--) { a[x][y] = count; count++; } x = round; for(y = n - 1 - round;y > round;y--) { a[x][y] = count; count++; } } if(n % 2 == 1) { a[n / 2][n / 2] = count; } } for(x = 0;x < n;x++) { for(y = 0;y < n;y++) { printf("%3d",a[x][y]); } printf("\n"); } printf("\n"); free(a); return 0;}
调试结果:
Please input the size of array:5 1 16 15 14 13 2 17 24 23 12 3 18 25 22 11 4 19 20 21 10 5 6 7 8 9
2、Z型蛇形输出矩阵
思路:首先根据要输出的矩阵的大小决定一次增加的数组值的最大个数num即n和循环的次数round(2 * n - 1),一次循环定义的二维数组值的下标x和y的和为round - 1,一直都是斜向上增加。
#include <stdio.h>#include <stdlib.h>int main(){ int x; int y; int n; int num; int round; int count = 1; printf("Please input the size of the array:\n"); scanf("%d",&n); int (*a)[n] = calloc(n * n,sizeof(int)); if(1 == n) { a[0][0] = count; } else { for(round = 1;round <= n;round++) { for(y = 0;y < round;y++) { a[round - 1 - y][y] = count; count++; } } for(;round <= 2 * n - 1;round++) { for(y = round - n;y < n;y++) { a[round - 1 - y][y] = count; count++; } } } for(x = 0;x < n;x++) { for(y = 0;y < n;y++) { printf("%3d",a[x][y]); } printf("\n"); } printf("\n"); free(a); return 0;}调试结果:
Please input the size of the array:7 1 3 6 10 15 21 28 2 5 9 14 20 27 34 4 8 13 19 26 33 39 7 12 18 25 32 38 43 11 17 24 31 37 42 46 16 23 30 36 41 45 48 22 29 35 40 44 47 49
0 0
- 二维数组蛇形和Z形矩阵输出
- 蛇形矩阵填写二维数组
- 不用数组输出蛇形矩阵
- c实现输出二维蛇形矩阵
- c语言实现输出二维蛇形矩阵
- 不借助二维数组,输出蛇形数组
- z形矩阵(蛇形矩阵)
- vector二维数组与蛇形矩阵
- 打印蛇形矩阵(二维数组)
- 二维数组的“蛇形打印”输出
- 蛇形矩阵(Z形矩阵),C++实现
- 【趣味题】输出蛇形矩阵(由内向外、由外向内)、输出Z字形矩阵。
- 填充Z形二维数组
- 二维数组 蛇形遍历
- 打印输出蛇形矩阵
- 蛇形矩阵的输出
- Java 输出蛇形矩阵
- 数组蛇形矩阵实现
- 2.Libgdx扩展学习之Box2D_刚体和形状
- 删除数组零元素
- 1058. 选择题(20)-有点难
- 1130/2016下午
- 小技巧——防止系统出问题文件丢失
- 二维数组蛇形和Z形矩阵输出
- 缓存监控工具
- SaaS企业想转型?也许SaaS + PaaS 模式是你们最后一次机会!
- Redis-低成本高可用方案设计Redis Sentinel
- java intern解析
- iDesktop、iObjects “打开数据库型数据源”出错解决方法
- JS模块化工具requirejs
- tomcat架构:Connector
- ajax将&特殊字符传到后台