给定一个n行m列的二维数组,左上角为1,蛇形递增,输出最后的数组
1 2 3
8 9 4
7 6 5
代码1(自己写的):
两种方法均借鉴了wrong的思路,wrong的代码中第二种方法只写了思路没有调试,所以出现了数据被覆盖,自己的代码完善了wrong写的第二种简易方法。
#include <iostream>
#include <stdio.h>
const int maxn = 100;
using namespace std;
int snake[maxn][maxn];
int n,m;
bool boundary(int r,int c)
{
if(r<1 || r>n || c<1 || c>m)
return 0;
else
return 1;
}
int main()
{
int r = 1;int c = 1;
int cot = 1;
scanf("%d%d",&n,&m);
int d = 0; //控制方向0、1、2、3
int dir[4][2] = {
{0,1},{1,0},{0,-1},{-1,0}};
while(cot<=n*m)
{
//方法一
/* while(cot<=n*m)
{
snake[r][c] = cot;
c++;
cot++;
if(!boundary(n,m) || snake[r][c])
{
r++;
c--;
break;
}
}

这篇博客介绍了如何在n行m列的二维数组中进行蛇形递增填充,从左上角1开始,如1 2 3, 8 9 4, 7 6 5所示。博主分享了自己的实现代码,包括两种方法,其中一种是对错误思路的修正和完善,另一种来自西交大学wrong学长的题目练习。"
122240425,11419673,STM32单片机从Flash读取音频数据播放实践,"['stm32', '音视频', '单片机', '嵌入式开发']
最低0.47元/天 解锁文章
4058

被折叠的 条评论
为什么被折叠?



