黑马程序员技术交流社区

标题: 7、写一方法,打印等长的二维数组,要求从1开始的自然... [打印本页]

作者: 小蔡@¥¥¥    时间: 2016-8-27 21:00
标题: 7、写一方法,打印等长的二维数组,要求从1开始的自然...


7、写一方法,打印等长的二维数组,要求从1开始的自然数由方阵的最外圈向内螺旋方式地顺序排列。

作者: 细听风语为梧桐    时间: 2016-8-27 22:16

public class Printx {
        public static void main(String[] args)
        {        
                setN(6);
        }
        
        public static void setN(int n)
        {
                int[][] arr=new int[n][n];
                int x=0;                                                        //用于记录每次的赋值
                int y=0;                                                        //用于改变每次赋值的起始点
                for(;n>=0;n--)
                {
                        x=give(arr,x,y,n);
                        give(arr,x,y,n);
                        y++;
  }
                printArr(arr);
        }
        
        public static void printArr(int[][] arr)//打印函数
        {
                for (int[] is : arr)
                {
                        for (int i : is)
                        {
                                if(i>=10)
                                        System.out.print(i+" ");
                                else
                                        System.out.print(" "+i+" ");
                        }
                        System.out.println();
                }
        }
   public static int give(int[][] arr,int x,int y,int n)//单圈赋值函数
        {
                for (int a=0;a<n;a++)
                {
                        if(arr[y][a]==0)
                                arr[y][a]=++x;
                }
                for (int a=0;a<n;a++)
                {
                        if(arr[a][n-1]==0)
                                arr[a][n-1]=++x;
                }
                for (int a=n-1;a>=0;a--)
                {
                        if(arr[n-1][a]==0)
                        {
                                arr[n-1][a]=++x;
                        }
  }
                for (int a=n-1;a>=0;a--)
                {
                        if(arr[a][y]==0)
                        {
                                arr[a][y]=++x;
                        }
                }
                return x;
        }
}
作者: 小超超    时间: 2016-8-27 22:32
用两个for循环嵌套
作者: ancheng    时间: 2016-8-27 23:26
[Java] 纯文本查看 复制代码
import java.util.Scanner;

/**
*
* @author AnCheng
*
*/
public class 回形取数 {

        private static Scanner sc;

        public static void main(String[] args) {

                sc = new Scanner(System.in);
                String[] str1 = sc.nextLine().split(" ");
                int x = Integer.parseInt(str1[0]);
                int y = Integer.parseInt(str1[1]);
               
                String[][] arr = new String[x][y];
                for (int i = 0; i < x; i++) {
                        String[] str = sc.nextLine().trim().split(" ");
                        arr =str;
                       
                }
                spiralOrderPrint(arr);
        }

        public static void spiralOrderPrint(String[][] matrix) {
                int tR = 0;
                int tC = 0;
                int dR = matrix.length - 1;
                int dC = matrix[0].length - 1;

                while (tR <= dR && tC <= dC) {
                        printEdge(matrix, tR++, tC++, dR--, dC--);
                }

        }

        public static void printEdge(String[][] m, int tR, int tC, int dR, int dC) {
                if (tR == dR) {
                        for (int i = tC; i <= dC; i++) {
                                System.out.print(m[tR] + " ");
                        }
                } else if (tC == dC) {
                        for (int i = tR; i <= dR; i++) {
                                System.out.print(m[tC] + " ");
                        }
                } else {
                        int curC = tC;
                        int curR = tR;
                        while (curR != dR) {
                                System.out.print(m[curR][tC] + " ");
                                curR++;
                        }
                        while (curC != dC) {
                                System.out.print(m[dR][curC] + " ");
                                curC++;
                        }
                        while (curR != tR) {
                                System.out.print(m[curR][dC] + " ");
                                curR--;
                        }
                        while (curC != tC) {
                                System.out.print(m[tR][curC] + " ");
                                curC--;
                        }
                }
        }
}

作者: ancheng    时间: 2016-8-27 23:27
[Java] 纯文本查看 复制代码
import java.util.Scanner;

public class Main {

        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                int m = sc.nextInt();
                int n = sc.nextInt();
                int[][] arr = new int[m][n];
                for (int i = 0; i < m; i++) {
                        for (int j = 0; j < n; j++) {
                                arr[j] = sc.nextInt();
                        }
                }
                sc.close();
                spiralOrderPrint(arr);
        }

        public static void spiralOrderPrint(int[][] m) {
                int r = 0;
                int c = 0;
                int tR = 0;
                int tC = 0;
                int dR = m.length - 1;
                int dC = m[0].length - 1;
                int n = m.length * m[0].length;
                for (int count = 0; count < n; tR++, tC++, dR--, dC--) {
                        while (r < dR && count <= n) {
                                System.out.print(m[r++][c] + " ");
                                count++;
                        }
                        while (c < dC && count < n) {
                                System.out.print(m[r][c++] + " ");
                                count++;
                        }
                        while (r > tR && count < n) {
                                System.out.print(m[r--][c] + " ");
                                count++;
                        }
                        while (c > tC && count < n) {
                                System.out.print(m[r][c--] + " ");
                                count++;
                        }
                        c++;
                        r++;
                }
        }

}





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