재귀 | 구구단

Algorithm 2019. 8. 27. 15:11
반응형
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
using System;
 
namespace Application
{
    //구구단 
    //n: 2 
    //result: 
    //2 * 9 = 18 
    //2 * 8 = 16
    //2 * 7 = 14
    //2 * 6 = 12
    //2 * 5 = 10
    //2 * 4 = 8
    //2 * 3 = 6
    //2 * 2 = 4
    //2 * 1 = 2
 
    class MainClass
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("구구단");
            var sol = new Solution();
            sol.solution(2);
        }
    }
 
    public class Solution { 
        public void solution(int n) {
            gugudan(n, 1);
        }
 
        public void gugudan(int n , int index) {
            if (index >= 9)
            {
                Console.WriteLine("{0} * {1} = {2}", n, index, n * index);
            }
            else {
                gugudan(n, index + 1);
                Console.WriteLine("{0} * {1} = {2}", n, index, n * index);
            }
        }
    }
}
 
 
 

 

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
using System;
 
namespace Application
{
    //구구단 
    //n: 2 
    //result: 
    //2 * 1 = 2
    //2 * 2 = 4
    //2 * 3 = 6
    //2 * 4 = 8
    //2 * 5 = 10
    //2 * 6 = 12
    //2 * 7 = 14
    //2 * 8 = 16
    //2 * 9 = 18 
 
    class MainClass
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("구구단");
            var sol = new Solution();
            sol.solution(2);
        }
    }
 
    public class Solution { 
        public void solution(int n) {
            gugudan(n, 1);
        }
 
        public void gugudan(int n , int index) {
 
            Console.WriteLine("{0} * {1} = {2}", n, index, n * index);
 
            if (index < 9)
            {
                gugudan(n, index + 1);
            }
 
        }
    }
}
 
 
 

 

 

반응형

'Algorithm' 카테고리의 다른 글

프로그래머스 | K번째수 | 정렬  (0) 2019.08.27
최단거리알고리즘  (0) 2019.08.27
재귀 | 최대값 구하기  (0) 2019.08.27
재귀 | 높은 자릿수  (0) 2019.08.27
재귀 | n까지의 합  (0) 2019.08.27
: