蛇形矩阵的c语言思路,蛇形矩阵输出是要怎么写代码,感觉没有眉目啊,大佬能说一下思想吗...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

#include

#include

#include

#include

using namespace std;

void rowfill(int& startnum, int& startrow,int& startcol

, int length,int step,int direct,vector> &vec) {

int i = 0;

for (; i < length; i++) {

vec[startrow][startcol+i*direct] = startnum +i*step;

}

startcol += i * direct;

startnum += i * step;

}

void colmfill(int &startnum, int&startrow,int& startcol

, int length,int step,int direct,vector> &vec) {

int i = 0;

for (; i < length; i++) {

vec[startrow+i*direct][startcol] = startnum + i * step;

}

startrow += i * direct;

startnum += i * step;

}

map> &)> >

filled= {

{0, rowfill}, {1, colmfill}, {2, rowfill}, {3, colmfill}

};

void draw(int m, int n, int p,int direct, vector> &vec) {

static int startrow = 0;

static int startcol = 0;

static int startnum = n ? 0 : (m * m - 1);

static int step = n ? 1: -1;

if (direct / 4) {

m-=2;

startrow++;

startcol++;

direct %= 4;

}

if (m<0) return;

int dir = (direct <2) ? 1: -1;

filled[(direct+(!p))%4](startnum, startrow, startcol, m-1, step, dir, vec);

if (m == 1) vec[startrow][startcol] = startnum;

draw(m, n, p, direct + 1, vec);

}

int main(){

int m, n, p;

cin >> m >> n >> p;

vector> vec(m,std::move(vector(m,0)));

draw(m, n, p, 0,vec);

for (auto &v : vec) {

for (auto c : v)

cout << c << '\t';

cout << endl;

}

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值