二维数组蛇形和Z形矩阵输出
2016-11-30 21:23
2406 查看
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,继续循环,最后判断一下输入的矩阵函数是奇数还是偶数,如果是奇数就不能忘了矩阵最中间的数,最后输出的数。矩阵的各个数都已经设定好了,就以依次打印出矩阵。
调试结果:
逆时针打印:
调试结果:
2、Z型蛇形输出矩阵
思路:首先根据要输出的矩阵的大小决定一次增加的数组值的最大个数num即n和循环的次数round(2 * n - 1),一次循环定义的二维数组值的下标x和y的和为round - 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) = 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) = 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) = 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
相关文章推荐
- 矩阵蛇形填数和顺时针输出
- 利用二维数组存储一个5行6列的单位矩阵,并按矩阵和转置矩阵形式输出
- 二维数组(矩阵)对角线输出
- 让ruby以矩阵的样式输出二维数组
- 利用二维数组存储一个3行4列的单位矩阵,并输出最大值和最小值
- 19.把1~100存到二维数组a[10][10]中,并按二维矩阵形式输出
- 蛇形矩阵填写二维数组
- java蛇形矩阵输出方法(顺时针、逆时针)
- 输出10*10蛇形排列矩阵
- 第6章 数组----遍历数组(使用二维数组输出一个3行4列且所有元素都是0的矩阵)
- 二维数组(矩阵)对角线输出
- 算法 - 蛇形矩阵输出
- 笔试题五:实现输出矩阵 二维数组
- 将对称矩阵压缩存储,实现矩阵相乘,输出相乘后结果(用二维数组)
- 不借助二维数组,输出蛇形数组
- 实验6 数组1 定义一个3×3的整形二维数组arr,为数组各元素赋值,找出最小值以及它的行下标和列下标,并输出该矩阵
- 顺时针绕圈输出二维数组(蛇形)
- 二维数组的“蛇形打印”输出
- 【面试】输出"蛇形"矩阵
- 【java】使用二维数组输出矩阵