티스토리 뷰

별찍기에서 가장 어려운 난이도인 다이아몬드(마름모) 출력하는 로직 입니다.

 

핵심로직1

공백개수 n개만큼 출력, * 개수 m개만큼 출력 하기 여기에서 맨 앞에 있는 파라메터 i는 디버깅용입니다.

private String getCharLine(int i, int spaceCnt, int charCnt) {
    return (mode ? i : "") + " ".repeat(spaceCnt) + "*".repeat(charCnt);
}

 

핵심로직2

i <= n / 2 인 경우

System.out.println(printDiamond.getCharLine(i, n/2 - i, 2 * i + 1));

 

i > n / 2 인 경우

System.out.println(printDiamond.getCharLine(i, i - (n/2), 2 * (n - i) - 1));

 

https://github.com/Areum120/java_study/blob/main/Ch4/src/practice/Q5.java

 

GitHub - Areum120/java_study

Contribute to Areum120/java_study development by creating an account on GitHub.

github.com

 

public class Q5 {

    String space = " ";
    String star = "*";
    int num;

    public Q5(){

    }

    public Q5(int n){
        this.num = n;
    }

    public void printStar(int n){
        int i,j;
        if(n>=2 || n%2==0) {//n이 최소 2보다 크고 짝수일 경우
            for (i = 0; i <= n; i++) {
                if (i <= n/2) {
                    for (j = 0; j <= (n/2) - i; j++) {
                        System.out.print(" ");
                    }
                    for (j = 1; j <= 2 * i + 1; j++) {
                        System.out.print("*");
                    }
                } else {
                    for (j = 0; j <= (i - (n/2)); j++) {
                        System.out.print(" ");
                    }
                    for (j = 1; j <= ((-2*i) + (2*n+1)); j++) {
                        System.out.print("*");
                    }
                }
                System.out.println();
            }
        }
        else{
            System.out.print("다이아몬드가 출력되지 않습니다.");
        }

    }

    public static void main(String[] args) {
        //반복문 사용하여 모양 출력
//         *
//        ***
//       *****
//      *******
//       *****
//        ***
//         *
        Q5 q5 = new Q5();
        q5.printStar(40);

    }
}

 

코드의 가독성 올리기 리팩토링

for (j = 0; j <= (n/2) - i; j++) {
    System.out.print(" ");
}
for (j = 1; j <= 2 * i + 1; j++) {
    System.out.print("*");
}

을 아래와 같이 공백개수와 *개수를 받아서 출력하게끔 리팩토링 하면 아래와 같이 만들 수 있습니다.

private String getCharLine(int i, int spaceCnt, int charCnt) {
    return (mode ? i : "") + " ".repeat(spaceCnt) + "*".repeat(charCnt);
}

 

 

를 리팩토링한 코드

 

public class PrintDiamond {
    private boolean mode = true;

    public PrintDiamond() {
    }

    public PrintDiamond(boolean mode) {
        this.mode = mode;
    }

    private String getCharLine(int i, int spaceCnt, int charCnt) {
        return (mode ? i : "") + " ".repeat(spaceCnt) + "*".repeat(charCnt);
    }
    public static void main(String[] args) {
        PrintDiamond printDiamond = new PrintDiamond(false);
        // space찍는 공식
        int n = 7;
        for (int i = 0; i < n; i++) { // 전체적으로 돌림
            if(i <= n / 2){ // 절반을 기준으로 로직이 달라짐
                // *이 1개일때 앞에 공백이 3
                System.out.println(printDiamond.getCharLine(i, n/2 - i, 2 * i + 1));
            } else {
                System.out.println(printDiamond.getCharLine(i, i - (n/2), 2 * (n - i) - 1));
            }
        }
    }
}

 

결과1 -- mode가 false인 경우

   *
  ***
 *****
*******
 *****
  ***
   *

 

결과2 -- mode가 true인 경우(default: true)

디버깅용

0   *
1  ***
2 *****
3*******
4 *****
5  ***
6   *

Process finished with exit code 0

'Language > 알고리즘' 카테고리의 다른 글

책 출간 했습니다 - 말랑말랑 알고리즘  (4) 2022.01.20
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함