SMALL

https://www.acmicpc.net/problem/1912

 

1912번: 연속합

첫째 줄에 정수 n(1 ≤ n ≤ 100,000)이 주어지고 둘째 줄에는 n개의 정수로 이루어진 수열이 주어진다. 수는 -1,000보다 크거나 같고, 1,000보다 작거나 같은 정수이다.

www.acmicpc.net


  • 문제


  • 문제풀이

첫번째 숫자를 output에 넣어주고 그다음 숫자를 더했을 때와 아닐때를 비교하여 max값을 찾는 방식으로 코드를 작성하였다.

 


  • 코드 1
using System;

class MainClass
{
    public static void Main(string[] args)
    {
        int n = int.Parse(Console.ReadLine());
        int[] input = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);

        int[] output = new int[n];
        output[0] = input[0]; 
        int max = output[0];

        for (int i = 1; i < n; i++)
        {

            output[i] = Math.Max(input[i], input[i] + output[i - 1]);
            max = Math.Max(max, output[i]);
        }

        Console.WriteLine(max);
    }
}

 


  • 후기

다이나믹 프로그래밍은 항상 접근하기 어렵다. 많이 노력해야겠다.

 

LIST

+ Recent posts