菱形

题目 菱形

image-350e185e

思路分析

abs(sx - i) + abs(sy - j) <= n / 2

代码实现

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int cx = n / 2, cy = n / 2;
        for (int i = 0; i < n; i ++ ) {
            for (int j = 0; j < n; j ++ ) {
                if (Math.abs(i - cx) + Math.abs(j - cy) <= n / 2)
                    System.out.printf("*");
                else
                    System.out.printf(" ");
            }
            System.out.println();
        }
    }
}

同类题型

视频讲解


项目分区导航约数 ⬅️ | 04-菱形 | ➡️ 递增序列