需要注意的是输出格式
- 后面的样板三角形顶部要先加空行
- 每一行输出最后一个字符后不能再继续输出空格
code
import java.util.Scanner;
public class p2091 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean flag = true;
while(sc.hasNext()) {
String str = sc.nextLine();
if("@".equals(str))
break;
char c = str.charAt(0);
int num = Integer.parseInt(str.split(" ")[1]);
if(flag)
flag = false;
else
System.out.println();
printTri(c, num);
}
sc.close();
}
public static void printTri(char c, int num) {
int len = 2*num-1;
int mid = len/2+1;
for(int i = 1;i<=num;i++) {
if(i == num)
for(int j = 1;j<=len;j++)
System.out.print(c);
for(int j = 1;j<=len;j++) {
if(i == 1) {
if(j == mid)
System.out.print(c);
else if(j>mid)
continue;
else
System.out.print(' ');
}else if(i > 1) {
if(j == mid - (i-1) || j == mid + (i-1))
System.out.print(c);
else if(j > mid + (i-1))
continue;
else
System.out.print(' ');
}
}
System.out.println();
}
}
}