使用曼哈顿距离画菱形

发布时间 2023-05-01 18:24:11作者: 逆袭怪

输入样例:

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 星期一