一道网易笔试题
2012-09-26 18:45
239 查看
如图:
设“1”的坐标为(0,0) “7”的坐标为(-1,-1) 编写一个小程序,使程序做到输入坐标(X,Y)之后显示出相应的数字。
原题的讨论见如下链接
/article/2410341.html
下面贴出我自己的解法,与大家分享:
/*
7 8 9 10
6 1 2 11
5 4 3 12
16 15 14 13
as the matrix shows, we set 1 with pos(0, 0), 2 with pos(1, 0)
3 with pos(1, 1), 4 with pos(0, 1) ....
I found that pos(1, 1) = 3 = 2*2-2+1, pos(2, 2) = 13 = 4*4-4+1
so the solution based on this is showed below
*/
//for input x, y, we return pos(x, y), pos(0, 0) = 1 ...
int GetVal(int x, int y)
{
//r means half of the width of the matrix,
// for 1 2
// 4 3, r = 2/2 = 1 ,
int r = max(abs(x), abs(y));
if(r == 0) return 1;
//rb means right and bottom number, here we mean 1, 3, 13 ...
//first we get the right and bottom number, then get others are easy
int rb = (2*r)*(2*r)-2*r+1;
if(x == r && y > -r ){ // the number over, eg. for rb = 13, number over // is 12 11 10 ...
return rb-(r-y);
}
if(y == -r){ //top level
return rb+4*r+(x-(-r));
}
if(y == r) //bottom level
return rb+r-x;
return rb+2*r+r-y;//left
}
设“1”的坐标为(0,0) “7”的坐标为(-1,-1) 编写一个小程序,使程序做到输入坐标(X,Y)之后显示出相应的数字。
原题的讨论见如下链接
/article/2410341.html
下面贴出我自己的解法,与大家分享:
/*
7 8 9 10
6 1 2 11
5 4 3 12
16 15 14 13
as the matrix shows, we set 1 with pos(0, 0), 2 with pos(1, 0)
3 with pos(1, 1), 4 with pos(0, 1) ....
I found that pos(1, 1) = 3 = 2*2-2+1, pos(2, 2) = 13 = 4*4-4+1
so the solution based on this is showed below
*/
//for input x, y, we return pos(x, y), pos(0, 0) = 1 ...
int GetVal(int x, int y)
{
//r means half of the width of the matrix,
// for 1 2
// 4 3, r = 2/2 = 1 ,
int r = max(abs(x), abs(y));
if(r == 0) return 1;
//rb means right and bottom number, here we mean 1, 3, 13 ...
//first we get the right and bottom number, then get others are easy
int rb = (2*r)*(2*r)-2*r+1;
if(x == r && y > -r ){ // the number over, eg. for rb = 13, number over // is 12 11 10 ...
return rb-(r-y);
}
if(y == -r){ //top level
return rb+4*r+(x-(-r));
}
if(y == r) //bottom level
return rb+r-x;
return rb+2*r+r-y;//left
}
相关文章推荐
- 一次数学比赛,共有A,B和C三道题目。所有人都至少解答出一道题目,总共有25人----网易游戏笔试题
- 一道网易笔试题
- 网易一道笔试题!
- 一道网易游戏笔试题的不同解法
- 网易的一道CSS笔试题--固定宽高的子元素如何在未知宽高的父元素中实现居中?
- 网易的一道笔试题的参考解法---关于广告牌投放问题
- 一道网易笔试题(加入最优解法)
- 网易的一道笔试题
- 网易笔试题一道
- 在CSDN上看到的一道有趣的网易笔试题目--附答案
- 一道网易笔试题目-几何相关
- 试题:网易笔试的一道题目
- 2014年网易互联网在线笔试题一道
- 网易的一道笔试题
- 网易2018校招内推Android笔试编程题一道
- 网易一道笔试题!
- 一道网易笔试题(加入最优解法)
- 一道多线程的笔试题
- 网易2018 Android实习生在线编程笔试题
- 网易游戏 从笔试到offer 经验分享