Skip to content

Instantly share code, notes, and snippets.

@hiheehee
Last active June 26, 2020 12:14
BOJ10993
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.Scanner;
public class BOJ10993 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
ArrayList<String> al = new ArrayList<>();
if(n == 1) { // n이 1일때 예외적으로 처리
System.out.println("*");
}else if(n == 2) { // n이 2일때 예외적으로 처리
System.out.println("*****");
System.out.println(" ***");
System.out.println(" *");
}else if(n >= 3) {
al.add("*****");
al.add("***");
al.add("*");
for(int i = 3; i <= n; i++) {
if(i%2 == 0) { // 짝수일때 (짝수 그림1)
//중간 별양쪽추가 (짝수 그림2)
al.add(al.size()-1, "*"+al.remove(al.size()-1)+"*");
int index = al.size()-1;
//아래 역삼각형 1개 추가 (짝수 그림3)
int count = al.get(al.size()-1).length()-4;
String empty = "";
for(; count >= 1;) {
empty = "";
for(int j = 0; j < count; j++) {
empty += " ";
}
count-=2;
al.add("*"+empty+"*");
}
al.add("*");
//위에 역삼각형 2개 추가 (짝수 그림4)
count = (al.get(index).length()- al.get(index-1).length())/2;
empty = "";
for(int k = index-1; k >= 0; k--) {
empty = "";
for(int j = 0; j < count; j++) {
empty += " ";
}
al.add(k, "*"+empty+al.remove(k)+empty+"*");
count += 2;
}
//맨위 별줄 추가 (짝수 그림5)
count = al.get(0).length() + 2;
String star = "";
for(int j = 0; j < count; j++) {
star += "*";
}
al.add(0, star);
}else {// 홀수일때 (홀수 그림 1)
//중간에 별양쪽 추가 (홀수 그림 2)
al.add(0,"*"+al.remove(0)+"*");
//밑에 정삼각형 2개 추가 (홀수 그림 3)
int count = (al.get(0).length()- al.get(1).length())/2;
String empty = "";
for(int k = 1; k < al.size(); k++) {
empty = "";
for(int j = 0; j < count; j++) {
empty += " ";
}
al.add(k, "*"+empty+al.remove(k)+empty+"*");
count += 2;
}
//맨밑 별줄 추가 (홀수 그림 4)
count = al.get(al.size()-1).length() + 2;
String star = "";
for(int j = 0; j < count; j++) {
star += "*";
}
al.add(star);
//위에 삼각형 1개 추가 (홀수 그림 5)
count = al.get(0).length()-4;
for(;count >= 1;) {
empty = "";
for(int j = 0; j < count; j++) {
empty += " ";
}
count -= 2;
al.add(0, "*"+empty+"*");
}
al.add(0,"*");
}
}
// 최종적으로 삼각형의 앞에 존재하는 공백을 추가한다.
int count = 0;
String empty ="";
if(n%2 == 0) { // n이 짝수일때, (짝수 그림 6)
for(int j = 0; j < al.size(); j++) {
empty = "";
for(int k = 0; k < count; k++) {
empty += " ";
}
al.add(j, empty+al.remove(j));
count++;
}
}else { // // n이 홀수일때, (홀수 그림 6)
count = 0;
for(int j = al.size()-1; j >= 0; j--) {
empty = "";
for(int k = 0; k < count; k++) {
empty += " ";
}
al.add(j, empty+al.remove(j));
count++;
}
}
//결과 출력
for(int i = 0; i < al.size(); i++) {
System.out.println(al.get(i));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment