재귀 | 최대값 구하기

Algorithm 2019. 8. 27. 14:18
반응형
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
using System;
 
namespace Application
{
    //n개의 정수를 입력 받아 최대값을 구하세요
    //n개의 정수 ( n <= 20 )
    //n : 5
    //input: 4 1 8 3 7 
    //result: 8 (최대값)
    //힌트 : (4,1,8,3,7)의 최대값 -> (4,1,8,3)의 최대값과 7중 큰 값 
 
    class MainClass
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("최대값 구하기");
            var sol = new Solution();
            int[] arr = { 4,1,8,3,7,11 };
            var result = sol.solution(arr);
            Console.WriteLine(result);
 
        }
    }
 
    public class Solution {
        public int solution(int[] arr) {
            return solution2(arr, 00);
        }
 
        public int solution2(int[] arr, int index, int num) { 
            if(index >= arr.Length) {
                return Math.Max(num, arr[index-1]);
            }
            return solution2(arr, index + 1Math.Max(num, arr[index]));
        }
    }
}
 
 
 
반응형

'Algorithm' 카테고리의 다른 글

최단거리알고리즘  (0) 2019.08.27
재귀 | 구구단  (0) 2019.08.27
재귀 | 높은 자릿수  (0) 2019.08.27
재귀 | n까지의 합  (0) 2019.08.27
프로그래머스 | 타겟넘버  (0) 2019.08.27
: