因为偶然间看到的一个题目
琢磨了一天想不出来,总是不完整,写的程序是死的。
在网上找也没有什么头绪。萌新求解
示例输入:3
得出
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1

解决方案 »

  1.   

    import java.util.Scanner;/**
     * @author momomian
     * @date 2019/10/22
     * @description
     */
    public class Test {    public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            int input = in.nextInt();
            create(input);
        }
        private static int getMinValue(int a,int b,int c,int d){
            return Math.min(Math.min(a,b),Math.min(c,d));
        }
        private static void create(int inputValue){
            int maxLen = 2 * inputValue - 1;
            for (int i = 1; i < maxLen+1; i++) {
                for (int j = 1; j < maxLen+1; j++) {
                    System.out.print(getMinValue(i,j,Math.abs(maxLen-i)+1,Math.abs(maxLen-j)+1));
                    System.out.print(" ");
                }
                System.out.println();
            }
        }
    }
      

  2.   

    public class test8 { public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner sc = new Scanner(System.in);
    System.out.println("请输入一个整数:");
    int n = sc.nextInt();
    for (int i = 1; i < 2 * n; i++) {
    for (int j = 1; j < 2 * n; j++) {
    if (i <= n) {
    if (j <= n) {
    if (j >= i) {
    System.out.print(i);
    } else {
    System.out.print(j);
    }
    } else {
    if (2 * n - j > i) {
    System.out.print(i);
    } else {
    System.out.print(2 * n - j);
    }
    }
    } else {
    if (j <= n) {
    if (j >= 2 * n - i) {
    System.out.print(2 * n - i);
    } else {
    System.out.print(j);
    }
    } else {
    if (2 * n - j > 2 * n - i) {
    System.out.print(2 * n - i);
    } else {
    System.out.print(2 * n - j);
    }
    }
    } }
    System.out.println();
    }
    }
    }