黑马程序员技术交流社区

标题: 螺旋矩阵 [打印本页]

作者: 晚上没饭吃    时间: 2015-8-12 00:56
标题: 螺旋矩阵
package answer;
import java.awt.print.Printable;
/***
*
* @author 周路博
*
*/
public class Test01 {
        public static void main(String[] args) {
                print(8);
        }
        public static void print(int len){
                int levels=len/2+len%2;
                int x=0;
                int y=0;
                int i=0;
                int count=1;
                int[][] arr = new int[len][len];
                while(i<levels ){
                        for(;y<len-i;y++ ){
                                arr[x][y]=count++;
                        }
                        y--;
                        x++;
                        for(;x<len-i;x++){
                                arr[x][y]=count++;
                        }
                        x--;
                        y--;
                        for(;y>=i;y--){
                                arr[x][y]=count++;
                        }
                        y++;
                        x--;
                        for(;x>i;x--){
                                arr[x][y]=count++;
                        }
                        x++;
                        y++;
                        i++;
                }
                for(int n=0;n<len;n++){
                        for(int j=0;j<len;j++){
                                System.out.print(arr[n][j]+"\t");
                        }
                        System.out.println();
                }
        }
}





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2