菱形
题目 菱形
思路分析
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();
}
}
}
💬 评论