A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ylw787442926 中级黑马   /  2016-5-28 17:23  /  1667 人查看  /  13 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

今天和朋友聊天,有个哥们提了一个挺巧妙地问题,把二维数组进行螺旋输出,做了一半思维有点乱,哪位大神可以教教小弟.

13 个回复

倒序浏览
说的再详细点
回复 使用道具 举报 1 0
就是一个二维数组 转着圈的输出
1 2 3 4 5 6 7
17 18 19   8
16  23 20  9
15  22  21 10
14 13  12  11
就这样按着序号输出,就是找个算法短一点的循环的输出一下,我知道一个一个输出就行,那样就没意思了
回复 使用道具 举报
然而这并没有什么意义
回复 使用道具 举报
My_Android 发表于 2016-5-28 21:36
然而这并没有什么意义

(⊙o⊙)…  你这样说让我很尴尬......这的确很扯
回复 使用道具 举报
二维数组,二维数组 每一位保存一个数字?
回复 使用道具 举报
有人会不,没人我来试试
回复 使用道具 举报
思路:循环存储,找外圈与内圈的规律,存储完后遍历二维数组就行了
回复 使用道具 举报
class Text1 {
        public static void main(String[] args){
                java.util.Scanner sc = new java.util.Scanner(System.in);
                System.out.println("请输入螺旋数组的长度:");
                int n = sc.nextInt();
                int number = 1;
                int[][] arr = new int[n][n];
               
                int i = 0;
                int j = 0;
                int count = 0;
                while (true) {
                        for (;j<arr[i].length-count;j++) {       
                                arr[i][j] = number;
                                number++;
                        }
                        j--;
                        i++;

                        for (;i<arr.length-count;i++) {
                                arr[i][j] = number;
                                number++;
                        }
                        i--;
                        j--;

                        for (;j>=count;j--) {
                                arr[i][j] = number;
                                number++;
                        }
                        j++;
                        i--;

                        for (;i>=count+1;i--) {
                                arr[i][j] = number;
                                number++;
                        }
                        i++;
                        j++;

                        count++;
                        if (count>=(n-1)/2+1) {
                                break;
                        }
                }
                System.out.println("--------------------------------");
                for (int x = 0;x<arr.length;x++) {
                        for (int y = 0;y<arr[x].length ;y++) {
                                System.out.print(arr[x][y]+"\t");
                        }
                        System.out.println("");
                }
        }
}
回复 使用道具 举报
方法1
public class Exercise {

        /**
         * @param args
         */
        public static void main(String[] args) {
                print(5);


        }
        public static void print(int n){
                int[][] a = new int[n][n];
                int hang=0;
                int lie=0;
                int num=1;
                int k=n;
               
               
                while(n>=1){
                        for (int i = 0; i < n; i++) {
                                a[hang][lie++]=num++;               
                        }
                       
                        lie--;
                        num--;
                        for (int j = 0; j < n; j++) {
                                a[hang++][lie]= num++;       
                        }
                        hang--;
                        num--;
                        for (int j2 = 0; j2 <n; j2++) {
                                a[hang][lie--]= num++;
                               
                        }
                       
                        lie++;
                        num--;
                       
                        for (int m = 0; m< n-1; m++) {
                                a[hang--][lie]= num++;
                               
                        }
                       
                        hang++;
                        lie++;
                        n-=2;
                }
               
                for (int i = 0; i < k; i++) {
                        for (int j = 0; j < k; j++) {
                                System.out.print(a[i][j]+" ");
                               
                        }
                        System.out.println();
                       
                }
               
        }
}
回复 使用道具 举报
方法2:
public static void main(String[] args) {
               
int[][] c = print(4,4);
for (int i = 0; i < c.length; i++) {
        for (int j = 0; j < c.length; j++) {
                System.out.print(c[i][j]+" ");
        }
        System.out.println();
}

        }
        public static int[][] print(int x, int n){
                int[][] a = new int[n][n];
               
                for (int i = 0; i < a.length; i++) {
                        for (int j = 0; j < a.length; j++) {
                                if (i==0||j==n-1){
                                        a[i][j]= x*x-n*n+ i+j+1;
                                }else if(i==n-1||j==0){
                                        a[i][j]=x*x-(n-2)*(n-2)+1-i-j;
                                }                       
                        }       
                }       
                if (n<=2){
                        return a;       
                }       
                int[][] a1 = print(x, n-2);
                for (int i = 1; i <= n-2; i++) {
                        for (int j = 1; j <= n-2; j++) {
                                a[i][j]=a1[i-1][j-1];       
                        }       
                }
                return a;       
        }
}
回复 使用道具 举报
何亚辉 发表于 2016-5-29 23:52
方法2:
public static void main(String[] args) {
               

谢谢给我发的 我复制下来运行看看
回复 使用道具 举报
我有上将潘凤 发表于 2016-5-29 23:48
class Text1 {
        public static void main(String[] args){
                java.util.Scanner sc = new java.util.Scanne ...

谢谢给我发的 我复制下来运行看看
回复 使用道具 举报
来学习下
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马