解决方案 »

  1.   

    http://topic.csdn.net/u/20080114/22/48153ab5-a7ea-4246-972b-5e4c9f868430.html这个挺全的
      

  2.   

    public class Main {    public static void main(String[] args) throws IOException {
            printf(2);
            System.out.println("----------");
            printf(6);
        }    static void printf(int i) {
            print(i < 3 ? 3 : i);//如题,i < 3时数组长宽为3
        }    static private void print(final int w) {
            int data[][] = new int[w][w];
            int len[] = new int[w];
            int count = 1;
            final int R = 0,  D = 1,  L = 2,  U = 3;//方向,右下左上
            int x = 0, y = 0, f = R;
            boolean flag = true;
            while (flag) {
                data[y][x] = count++;
                if (len[x] < String.valueOf(data[y][x]).length()) {
                    len[x] = String.valueOf(data[y][x]).length();
                }
                switch (f) {
                    case R:
                        if (w == x + 1) {//碰到边缘,转向,移位
                            f = D;
                            y++;
                        } else if (/*data[y][x+1]>0&&*/data[y + 1][x] > 0) {//所有都赋值完则结束
                            flag = false;
                        } else if (data[y][x + 1] > 0) {//碰到已赋值的,转向,移位
                            f = D;
                            y++;
                        } else {
                            x++;//向右移
                        }
                        break;
                    case D:
                        if (w == y + 1) {
                            f = L;
                            x--;
                        } else if (data[y][x - 1] > 0) {
                            flag = false;
                        } else if (data[y + 1][x] > 0) {
                            f = L;
                            x--;
                        } else {
                            y++;
                        }
                        break;
                    case L:
                        if (x == 0) {
                            f = U;
                            y--;
                        } else if (data[y - 1][x] > 0) {
                            flag = false;
                        } else if (data[y][x - 1] > 0) {
                            f = U;
                            y--;
                        } else {
                            x--;
                        }
                        break;
                    case U:
                        if (data[y][x + 1] > 0) {
                            flag = false;
                        } else if (data[y - 1][x] > 0) {
                            f = R;
                            x++;
                        } else {
                            y--;
                        }
                        break;
                }
            }
            String buf = "";
            for(int h=0;h<w;h++){//输出
                for(int l=0;l<w;l++){
                    System.out.print(data[h][l]);
                    buf=String.valueOf(data[h][l]);
                    System.out.print(getRT(2+len[l]-buf.length()));//输出空格
                }
                System.out.println();
            }
        }
        static String getRT(int i){
            return i<2?" ":" "+getRT(i-1);
        }
    }
    /*
    run:
    1  2  3  
    8  9  4  
    7  6  5  
    ----------
    1   2   3   4   5   6   
    20  21  22  23  24  7   
    19  32  33  34  25  8   
    18  31  36  35  26  9   
    17  30  29  28  27  10  
    16  15  14  13  12  11  
    成功生成(总时间:0 秒)*/
      

  3.   

        public static void main(String[] args) {
            String reg = "seo";
            String replace = "8888";
            String str = "aaa seo kkk  seo ";
            System.out.println(replaceFirst(str, reg, replace));
        }    public static String replaceFirst(String str, String from, String to) {
            int len = from.length();
            int offset = str.indexOf(from);
            if (offset < 0)
                return str;
            StringBuffer sb = new StringBuffer();
            sb.append(str.substring(0, offset));
            sb.append(to);
            sb.append(str.substring(offset + len));
            return sb.toString();
        }http://topic.csdn.net/u/20091230/13/500d5998-93d7-4ec5-aebc-4d7a15d19efa.html
      

  4.   

    上面发错了……http://topic.csdn.net/u/20091222/10/57f58c73-7422-4755-a64b-a803a920eea5.htmlpublic class Matrix {
    private int[][] matrix;
    private int n; public Matrix(int n) {
    this.n = n;
    matrix = new int[n][n];
    int[] direct = new int[] { 1, 0, -1, 0 };
    int directX = 0, directY = 3, currentX = -1, currentY = 0;
    for (int count = 0; count < n * n;) {
    int nextX = currentX + direct[directX], nextY = currentY
    + direct[directY];
    if (nextX >= n || nextY >= n || nextX < 0 || nextY < 0
    || matrix[nextY][nextX] != 0) {
    directX = (directX + 1) % 4;
    directY = (directY + 1) % 4;
    continue;
    }
    matrix[nextY][nextX] = ++count;
    currentX = nextX;
    currentY = nextY;
    }
    } public String toString() {
    StringBuffer sb = new StringBuffer();
    if (matrix != null) {
    int length = new String(n * n + "").length();
    String format = "%-" + length + "d ";
    for (int[] array : matrix) {
    for (int i : array) {
    sb.append(String.format(format, i));
    }
    sb.append('\n');
    }
    }
    sb.trimToSize();
    return sb.toString();
    }
    }
      

  5.   

    后来弄了弄,结果是对的
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;public class Test {
    public static void main(String[] args) {
    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);
    int leng = 1;
    int[] square;  //将那些数用一维数组存放
    String dir = "RIGHT"; //方向 初值向右
    int dirNum = 1; //方向调转次数
    int i = -1;  //数组的下标
    int j = 0;  //控制前进长度
    int value = 1;  //数值
    int round = 1;  //圈数 System.out.println("请输入 顺时针图形边长:"); try {
    leng = Integer.parseInt(br.readLine());
    } catch (NumberFormatException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } square = new int[leng * leng]; while (dirNum < leng * 2) {
    if (round > 0) {
    j = 1 - round;
    }
    if (dir == "RIGHT") {

    for (; j < leng - round + 1; j++) {
    i++;
    square[i] = value;
    value++;
    if (square[i + 1] != 0) {
    break;
    }

    }
    dir = changeDir(dir, dirNum);
    dirNum++; } else if (dir == "DOWN") {
    i = i + leng;
    for (; j < leng - round; j++) {
    square[i] = value;
    value++;
    if (i == square.length - 1) {
    break;
    }
    if (square[i + leng] != 0) {
    break;
    }
    i = i + leng;
    }
    dir = changeDir(dir, dirNum);
    dirNum++;
    } else if (dir == "LEFT") {
    i = i - 1;
    for (; j < leng - round; j++) {
    square[i] = value;
    value++;
    if (square[i - 1] != 0) {
    break;
    }
    i = i - 1;
    }
    dir = changeDir(dir, dirNum);
    dirNum++;
    } else if (dir == "UP") {
    i = i - leng;
    for (; j < leng - round - 1; j++) {
    square[i] = value;
    value++;
    if (square[i - leng] != 0) {
    break;
    }
    i = i - leng;
    }
    dir = changeDir(dir, dirNum);
    dirNum++;
    round++;
    }
    } print(square, leng);
    } private static void print(int[] square, int leng) {
    for (int m = 0; m < square.length; m++) { System.out.print(square[m]); }
    } private static String changeDir(String dir, int dirNum) {
    if (dir == "RIGHT") {
    return "DOWN";
    } else if (dir == "DOWN") {
    return "LEFT";
    } else if (dir == "LEFT") {
    return "UP";
    } else if (dir == "UP") {
    return "RIGHT";
    }
    return dir;
    }
    }
      

  6.   

    上面输出没改,不好意思!
    private static void print(int[] square, int leng) {
    for (int m = 0; m < square.length; m++) {
    if(m%leng == 0) {
    System.out.println();
    System.out.print(square[m]);
    }else {
    System.out.print(square[m]);
    }
    }
    }
    输入3时,结果:请输入 顺时针图形边长:
    3123
    894
    765输入4时,结果:请输入 顺时针图形边长:
    41234
    1213145
    1116156
    10987输入6时,结果:请输入 顺时针图形边长:
    6123456
    20212223247
    19323334258
    18313635269
    173029282710
    161514131211