螺旋数字矩阵
2009-05-05 17:43
302 查看
package testing; /** * 43 44 45 46 47 48 49 50 * 42 21 22 23 24 25 26 . * 41 20 7 8 9 10 27 . * 40 19 6 1 2 11 28 . * 39 18 5 4 3 12 29 * 38 17 16 15 14 13 30 * 37 36 35 34 33 32 31 * * 给出一个坐标,获得螺旋数字矩阵中的数值。(1 为点(0,0)正负方向为二维坐标轴方向) */ public class SpiralData { public static int getValueInSpiralMatrix(int x, int y) { if ((x >= 0) && (y >= 0) && (x == y)) {//第一象限对角线的情况,直接返回结果 return (2 * x + 1) * (2 * x + 1); }else if(Math.abs(x) >= Math.abs(y)){//x的绝对值大于y的绝对值的情况 if(x <= 0){//x在二三象限时,计算出左边x那列中间的数值根据y进行调整 return ((2 * Math.abs(x) + 1) * (2 * Math.abs(x) + 1) - (2 * Math.abs(x)) - (2 * Math.abs(x) + 1)/2) + y; }else{//x在一四象限时,计算出右边x那列中间的数值根据y进行调整 return ((2 * Math.abs(x) + 1) * (2 * Math.abs(x) + 1) - 3 * (2 * Math.abs(x)) - (2 * Math.abs(x) + 1)/2) - y; } }else{//x的绝对值小于y的绝对值的情况 if(y >= 0){//和上一种情况类似 return ((2 * Math.abs(y) + 1) * (2 * Math.abs(y) + 1) - (2 * Math.abs(y) + 1)/2) + x; }else{ return ((2 * Math.abs(y) + 1) * (2 * Math.abs(y) + 1) - 2 * (2 * Math.abs(y)) - (2 * Math.abs(y) + 1)/2) - x; } } } public static void main(String[] args) { for (int i = -3; i <= 3; i++) { for(int j = -3; j <= 3; j++) System.out.println(getValueInSpiralMatrix(i, j)); } } }
相关文章推荐
- 经典算法(四) 数组相关 & 螺旋矩阵 & 数字大小写转换
- 1,2,3…n*n 的数字按照顺时针螺旋的形式打印成矩阵(递归)
- 从键盘输入一个整数(1~20) 则以该数字为矩阵的大小,把1,2,3…n*n 的数字按照顺时针螺旋的形式填入其中。
- 1,2,3…n*n 的数字按照顺时针螺旋的形式打印成矩阵(递归)
- 打印螺旋数字矩阵
- 打印螺旋数字矩阵
- 顺时针螺旋输出矩阵中的1到n*n的数字
- xmu 1182.螺旋数字矩阵
- 以该数字为矩阵的大小,把1,2,3…n*n 的数字按照顺时针螺旋的形式填入其中
- 螺旋矩阵求指定位置的数字
- 螺旋数字矩阵的输出
- 从键盘输入一个整数(1~20) 则以该数字为矩阵的大小,把1,2,3…n*n 的数字按照顺时针螺旋的形式填入其中。
- 递归:求螺旋排列数字的矩阵
- 1050. 螺旋矩阵(25)
- [PAT乙级]1050. 螺旋矩阵(25)
- 螺旋矩阵
- PAT BASIC LEVEL 1050. 螺旋矩阵(25)
- 螺旋矩阵及其变种
- 螺旋矩阵_动态规划解法
- 数字矩阵 动态规划