Hangzhou Electric 2091 hollow triangle Java (AC)

Topic: http://acm.hdu.edu.cn/showproblem.php?pid=2091

The triangle write two-dimensional array, and then output it

Precautions:

No spaces (each layer later) 1. triangular back

2. Empty lines between the triangle;

3.n = 1, special treatment, because the first line is output separately.

import java.util.Scanner;

public class Main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean b = true;
while (input.hasNext()){
String s = input.next();
if (s.charAt(0) == '@'){
break;
}
int n = input.nextInt();
if (b){
b = false;
}else {
System.out.println();
}
int col = 2*n-1;
String strs[][] = new String[n][col];
for (int i = 0;i < n;i++){
strs[i][col/2-i] = s;
strs[i][col/2+i] = s;
}
for (int i = 0;i < col;i++){
strs[n-1][i] = s;
}
int t = 0;
if (n == 1){
System.out.println(s);
}else {
for (int i = 0;i <= col/2;i++){
if (strs[0][i] == null) {
System.out.print(" ");
}else {
System.out.println(s);
}
}
for (int i = 1;i < n-1;i++){
t = 0;
for (int j = 0;j < col;j++){
if (strs[i][j] != null) {
System.out.print(s);
t++;
}else {
System.out.print(" ");
}
if (t == 2){
break;
}
}
System.out.println();
}
for (int i = 0;i < col;i++){
System.out.print(strs[n-1][i]);
}
System.out.println();
}

}
}
}

Guess you like

Origin www.cnblogs.com/ztabk/p/12297163.html