应届生求职网小程序
查看: 4741|回复: 0
打印 上一主题 下一主题

发autodesk矩阵题答案

[复制链接]
吃吃      

主题

好友

134万

积分

世界首富

跳转到指定楼层
1
发表于 2008-3-17 10:33 |显示全部楼层 |倒序浏览

#include "iostream"
using namespace std;

#define RIGHT 1
#define DOWN 2
#define LEFT 3
#define UP 4

int judge(int step, int walk, int flag)
{
if( walk < step )return flag;
switch(flag)
{
case RIGHT:
return DOWN;
case DOWN:
return LEFT;
case LEFT:
return UP;
case UP:
return RIGHT;
}
}

void matrix(int N)
{
int length = N * N;
int *p = new int[length];
int flag = RIGHT;
int step = N;
int count = 0;
int walk = -1;
for(int i = 0 ; i < length ; i++)
{
walk += 1;
int newflag = judge(step , walk , flag);

if(flag != newflag)
{
count += 1;
flag = newflag;
walk = 0;
if( (count + 1) % 2 == 0 )
{
step -= 1;
}
}


switch(flag)
{
//right
case RIGHT:
if(i == 0)
p[i] = 0;
else
p[i] = p[i - 1] + 1;
break;
//down
case DOWN:
p[i] = p[i - 1] + N;
break;
//left
case LEFT:
p[i] = p[i - 1] - 1;
break;
//up
case UP:
p[i] = p[i - 1] -N;
}

}

int *tmp = new int[length];

for(int j = 0; j < length; j++)
{
tmp[p[j]] = j + 1;
}

for(j = 0; j < length; j++)
{
cout << tmp[j] << " ";
if( ((j % N) == (N - 1)) )cout << "
";
}

}



void main()
{
matrix(7);
system("pause");

}

+10
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册 QQ登录

本版积分规则

关闭

站长推荐上一条 /1 下一条

应届生微信小程序|应届生求职网YingJieSheng.COM ( 沪ICP备12015550号-13 )

GMT+8, 2024-11-23 16:17

Powered by Discuz!

© 2001-2012 Comsenz Inc.

快速回复 返回顶部 返回列表