--- title: "04-菱形" created: 2025-12-02 tags: - 项目 aliases: - 菱形 --- # 菱形 ## 题目 [菱形](https://www.acwing.com/problem/content/729/) ![[image-350e185e.png]] ## 思路分析 abs(sx - i) + abs(sy - j) <= n / 2 ## 代码实现 ```java 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(); } } } ``` ## 同类题型 ## 视频讲解 --- **项目分区导航**: [[03-约数|约数]] ⬅️ | 04-菱形 | ➡️ [[05-递增序列|递增序列]]