package studyJava; public class Main { public static void trsp(int n){ if(n == 0) return; int sum = (n + n*(n-1)/2)*2; int count = n; int low = 1; int high = sum + 1 - count; int highstr = high; int flagcount; StringBuilder strb = new StringBuilder(); for(int i=0; i<n; i++){ flagcount = i*2; if(flagcount != 0){ for(int f=0; f<flagcount; f++){ strb.append("-"); } } for(int ll = 0; ll < count; ll++){ strb.append(String.valueOf(low++)).append("*"); } for(int hh = 0; hh < count-1; hh++){ strb.append(String.valueOf(high++)).append("*"); } strb.append(String.valueOf(high++)); strb.append(" "); count--; high = highstr - count; highstr = high; } System.out.println(strb.toString()); } public static void main(String args[]){ trsp(4); } }
结果:
1*2*3*4*17*18*19*20 --5*6*7*14*15*16 ----8*9*12*13 ------10*11