题解 | #牛牛的字符菱形#
牛牛的字符菱形
https://www.nowcoder.com/practice/11a5040fa894430f9d25fda1be37ddc8
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); char ch = in.next().charAt(0); int n = 5; int centerX = n / 2; int centerY = n / 2; for(int i = 0; i < n; ++i){ for(int j = 0; j < n; ++j){ if(Math.abs(i - centerX) + Math.abs(j - centerY) <= n / 2) System.out.print(ch); else System.out.print(" "); } System.out.println(); } } }
曼哈顿距离解法 借鉴了这个博主https://blog.csdn.net/Weraphael/article/details/128595858