关于java中普通算法求解

img


我知道先做一边然后采用反转做出下部分,但我无奈不知道上部分如何做
求做法

这是我的代码,如有问题欢迎指正。

如果可以的话请关注我的博客,会定时更新一些简单的算法问题与学习笔记。

祝读者学业进步!


代码:

package Myanswer;

import java.util.Scanner;

public class Solution {

    public static void Print(int n) {
        int row = n;
        int space = 0;
        if (n <= 0 || n >= 10) {
            System.out.println("Your input is out of bound!");
            return;
        }
        while (row >= 1) {
            for (int j = 0; j < space; j++)
                System.out.print(" ");
            for (int i = 0; i < (2 * row - 1); i++)
                System.out.print(row);
            System.out.println("");
            row--;
            space++;
        }
        row = 2;
        space -= 2;
        while (row <= n) {
            for (int j = 0; j < space; j++)
                System.out.print(" ");
            for (int i = 0; i < (2 * row - 1); i++)
                System.out.print(row);
            System.out.println("");
            row++;
            space--;
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        Solution.Print(n);
    }

}

运行截图:

img