最近,一直在BS,然后就碰到了这样的问题,
数组如下:
A B C D E
7 J K L 1
6 P Q 8 2
5 O N 9 3
I H G F 4
要求输出,如下结果:ABCDE1234FGHI567JKL89NOPQ
我的算法是:
void function(char[][] in)
{
int columns=in[0].length,rows=in.length;
int i=0,j=0,k=1,count=0;
int size=columns*rows;
while(count<size)
{
while(j<columns-k)
{
System.out.print(in[i][j--]);
count++;
}
while(i<rows-k)
{
System.out.print(in[i++][j]);
count++;
}
while(j>=k)
{
System.out.prin(in[i][j--]);
count++;
}
while(i>k)
{
System.out.print(in[i++][j]);
count++;
}
}}

解决方案 »

  1.   

    使用递归输出:            public static void testPrint(String[][] arr){
    int len = arr.length;
    int deep = arr[0].length;
    if(len == 1){
    for(int i = 0; i < deep; i ++){
    System.out.print(arr[0][i]);
    }
    return;
    }
    if(deep == 1){
    for(int i = 0; i < len; i ++){
    System.out.print(arr[i][0]);
    }
    return;
    }
    if(len == 2){
    System.out.println(Arrays.toString(arr[0]));
    for(int i = 0; i < deep; i ++){
    System.out.print(arr[1][deep-1-i]);
    }
    return;
    }
    if(deep == 2){
    for(int i = 0; i < len; i ++){
    System.out.print(arr[i][0]);
    }
    for(int i = 0; i < len; i ++){
    System.out.print(arr[len- 1 - i][1]);
    }
    return;
    }
    for(int i = 0; i < deep; i ++){//上
    System.out.print(arr[0][i]);
    }
    for(int i = 1; i < len; i ++){//右
    System.out.print(arr[i][len-1]);
    }
    for(int i = 0; i < deep-1; i ++){//下
    System.out.print(arr[len-1][deep-2-i]);
    }
    for(int i = 1; i < len-1; i ++){//左
    System.out.print(arr[len-1-i][0]);
    }

    String[][] tmp = new String[len - 2][deep - 2];
    for(int i = 0; i < len - 2; i ++){
    for(int j = 0; j < deep - 2; j ++){
    tmp[i][j] = arr[i+1][j+1];
    }
    }
    testPrint(tmp);
        }
                public static void main(String[] args) {
    String[][] arr = {{"A","B","C","D","E"},
               {"7","J","K","L","1"},
               {"6","P","Q","8","2"},
               {"5","O","N","9","3"},
               {"I","H","G","F","4"}};
    testPrint(arr);
    }
    结果:
    ABCDE1234FGHI567JKL89NOPQ
      

  2.   

    凑热闹:
    1. 楼主的代码根本就不能运行通过
    2. 写了一个public static void function2(char[][] in) {
    int columns = in[0].length;
    int rows = in.length;
    int size = columns * rows;
    int x = 0;
    int y = 0;
    int xStep = 1;
    int yStep = 0;
    int xMax = columns-1;
    int yMax = rows-1;
    int xMin = 0;
    int yMin = 0; char[] output = new char[size];
    int count = 0;
    while (count < size) {
    output[count++] = in[y][x];
    System.out.print(count);
    System.out.printf(":x=%d,y=%d,char=%s\n", x, y, output[count-1]);
    if (yStep == 0) {
    if (xStep > 0 && x == xMax) { // forward
    // change direction
    yStep = xStep;
    yMin++;
    y++;
    xStep = 0;
    }
    else if (xStep < 0 && x == xMin) { // backward
    yStep = xStep;
    yMax--;
    y--;
    xStep = 0;
    }
    else {
    // y stays the same, process row
    x += xStep;
    }
    }
    else {// if (xStep == 0) {
    if (yStep > 0 && y == yMax) { // forward
    xStep = -yStep;
    xMax--;
    x--;
    yStep = 0;
    }
    else if (yStep < 0 && y == yMin) {
    xStep = -yStep;
    xMin++;
    x++;
    yStep = 0;
    }
    else {
    y += yStep;
    }
    }
    }
    System.out.println(new String(output));
    }
      

  3.   

    一个小算法。支持矩形,支持顺逆时针方向旋转    public static void v(Object[][] in, boolean boo) {
            int a, b;
            if (boo) {
                a = in[0].length;
                b = in.length;
            } else {
                a = in.length;
                b = in[0].length;
            }
            int[][] _arr = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
            int[] __arr = {a, b - 1, a - 1, b - 2};
            int k = 0;
            int x = 0, y = -1;
            while (k < a * b) {
                for (int i = 0; i < _arr.length; i++) {
                    for (int j = 0; j < __arr[i]; j++) {
                        y += _arr[i][0];
                        x += _arr[i][1];
                        k++;
                        System.out.print(boo ? in[x][y] : in[y][x]);
                    }
                    __arr[i] -= 2;
                }
            }
        }
      

  4.   

    增强版    public static void v(Object[][] in, boolean boo) {
            int a, b;
            if (boo) {
                a = in[0].length;
                b = in.length;
            } else {
                a = in.length;
                b = in[0].length;
            }
            int[][] _arr = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
            int[] __arr = {a, b - 1, a - 1, b - 2};
            int x = 0, y = -1;
            int k = 0;
            while (true) {
                for (int i = 0; i < _arr.length; i++) {
                    for (int j = 0; j < __arr[i]; j++) {
                        y += _arr[i][0];
                        x += _arr[i][1];
                        k++;
                        System.out.print(boo ? in[x][y] : in[y][x]);
                    }
                    if (k >= a * b) {
                        return;
                    }
                    __arr[i] -= 2;
                }
            }
        }