给定一个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,