使用曼哈顿距离画菱形

输入样例:

5
输出样例:

  *  
 *** 
*****
 *** 
  * 
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 ++){
                int d = Math.abs(i - cx) + Math.abs(j - cy);
                if (d <= n / 2)
                    System.out.printf("*");
                else 
                    System.out.printf(" ");
            }
            System.out.println();
        }
    }
}

2023-05-01 18:15:17 星期一

posted on 2023-05-01 18:15  逆袭怪  阅读(20)  评论(0编辑  收藏  举报