본문 바로가기
알고리즘

Do it! 자료구조와 함께 배우는 알고리즘 입문 (자바편) - 직각 이등변 삼각형 출력(Q15~17)

by Love of fate 2021. 9. 4.
728x90
반응형

* Q15 : 직각 이등변 삼각형을 출력하는 부분을 아래와 같은 형식의 메서드로 작성하세요 

  - static void triangleLB(int n)

  - 또 왼쪽 위, 오른쪽 위, 오른쪽 아래가 직각인 이등변 삼각형을 출력하는 메서드를 작성하세요.

    - static void triangleLU(int n)  //왼쪽 위가 직각인 이등변 삼각형을 출력  

    - static void triangleRU(int n)  //오른쪽 위가 직각인 이등변 삼각형을 출력

    - static void triangleRB(int n)  //오른쪽 아래가 직각인 이등변 삼각형을 출력

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package algorithm;
 
import java.util.Scanner;
 
public class TriangleLBTest15 {
 
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        
        int n;
        
        System.out.println("왼쪽 아래가 직각인 이등변 삼각형을 출력합니다.");
        
        do {
            
            System.out.print("몇 단 삼각형입니까?");
            n = sc.nextInt();
            
        }while(n <= 0);
        
        System.out.println();
        triangleLB(n); //왼쪽 아래가 직각인 삼각형
        
        System.out.println();
        triangleLU(n); //왼쪽 위가 직각인 삼각형
        
        System.out.println();
        triangleRU(n); //오른쪽 위가 직각인 삼각형
        
        System.out.println();
        triangleRB(n); //오른쪽 아래가 직각인 삼각형
 
    }
    
    
    public static void triangleLB(int n) {
        
        for(int i=1; i <= n; i++) {
            for(int j=1; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
    
    public static void triangleLU(int n) {
        
        for(int i=1; i <= n; i++) {
            for(int j=(n-(i-1)); j >= 1; j--) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
    
    public static void triangleRU(int n) {
        StringBuffer sb = null;
        for(int i=1; i <= n; i++) {
             
            sb = new StringBuffer(n);
 
            for(int k = 1; k < i; k++) {
                 sb.append(" ");
            }
            
            for(int j=n-(i-1); j >= 1; j--) {
                
                sb.append("*");
            }
            System.out.println(sb.toString());
        }
    }
    
    public static void triangleRB(int n) {
        
        StringBuffer sb = null;
        
        for(int i=1; i <= n; i++) {
            
            sb = new StringBuffer(n);
            
            for(int k = 1; k <= n-i; k++) {
                 sb.append(" ");
            }
            
            for(int j=1; j <= i; j++) {
                sb.append("*");
            }
            
            System.out.println(sb.toString());
        }
    }
}
 
cs


* Q16 : n단의 피라미드를 출력하는 메서드를 작성하세요 

  - static void spira(int n)

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package algorithm;
 
import java.util.Scanner;
 
public class TriangleLBTest16 {
 
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        
        int n;
        
        System.out.println("n단의 피라미드를 출력합니다.");
        
        do {
            
            System.out.print("몇 단의 피라미드 입니까?");
            n = sc.nextInt();
            
        }while(n <= 0);
        
        System.out.println();
        spira(n);
    }
    
    static void spira(int n) {
        
        StringBuffer sb;
        
        for(int i=1; i <= n; i++) {
            
            sb = new StringBuffer(n);
            
            for(int j = 1; j <= n-i; j++) {
                sb.append(" ");
            }
            for(int k = 1; k <= (i-1)*2+1; k++) {
                sb.append("*");
            }
            System.out.println(sb.toString());
        }
    };
}
 
cs


* Q17 : 아래를 향한 n단의 숫자 피라미드를 출력하는 메서드를 작성하세요.

  - static void npira(int n)

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package algorithm;
 
import java.util.Scanner;
 
public class TriangleLBTest16 {
 
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        
        int n;
        
        System.out.println("n단의 숫자 피라미드를 출력합니다.");
        
        do {
            
            System.out.print("몇 단의 숫자 피라미드 입니까?");
            n = sc.nextInt();
            
        }while(n <= 0);
        
        System.out.println();
        spira(n);
    }
    
    static void spira(int n) {
        
        StringBuffer sb;
        
        for(int i=1; i <= n; i++) {
            
            sb = new StringBuffer(n);
            
            for(int j = 1; j <= n-i; j++) {
                sb.append(" ");
            }
            
            for(int k = 1; k <= (i-1)*2+1; k++) {
                sb.append(i);
            }
            System.out.println(sb.toString());
        }
    };
}
 
cs

728x90
반응형