使用曼哈顿距离画菱形
输入样例:
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 星期一
本文来自博客园,作者:逆袭怪,转载请注明原文链接:https://www.cnblogs.com/fghjktgbijn/p/17366809.html