以蛇型图案从 C 程序最后一列打印矩阵。
给定一个 nxn 大小的数组,该程序必须从最后一列开始,以蛇型图案打印数组的元素,即从 arr[0][n] 第 n 个元素开始,且不会对其原始位置进行任何更改。

例如
Input: arr[]= 100 99 98 97 93 94 95 96 92 91 90 89 85 86 87 88 Output: 97 98 99 100 96 95 94 93 92 91 90 89 88 87 86 85
算法
START Step 1 -> declare initial variable as int n to 5, i and j Step 2 -> declare array of 2-D matrix with elements Step 3 -> Loop For i=0 and i<n and i++ IF i%2==1 Loop For j=0 and j<n and j++ Print arr[i][j] End End Else Loop For j=n-1 and j>=0 and j-- Print arr[i][j] End End STOP
例如
#include <stdio.h>
int main(){
int n = 5;
int arr[][5]= {
{10, 20, 30, 40, 50},
{ 60, 70, 80, 90, 100 },
{ 110, 120, 130, 140, 150 },
{ 160, 170, 180, 190, 200 },
{ 210, 220, 230, 240, 250 }
};
for (int i = 0; i < n; i++) { //from column as we must start from the end
if (i%2 == 1)
for (int j = 0; j < n; j++)
printf("%d ", arr[i][j]);
else
for (int j = n - 1; j >= 0; j--)
printf("%d ", arr[i][j]);
}
return 0;
}输出
如果运行上述程序,则它将生成以下输出
50 40 30 20 10 60 70 80 90 100 150 140 130 120 110 160 170 180 190 200 250 240 230 220 210
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程语言
C++
C#
MongoDB
MySQL
Javascript
PHP