알고리즘/BOJ

백준 10211번 Maximum Subarray

꾸준함. 2019. 2. 8. 00:44

문제 링크입니다: https://www.acmicpc.net/problem/10211


간단한 부분합 문제였는데 주의할 점은 길이가 1인 즉, 원소가 하나인 부분합도 고려해야합니다!


#include <iostream>

#include <algorithm>

#include <climits>

using namespace std;

 

const int MAX = 1000 + 1;

 

int pSum[MAX];

 

int main(void)

{

        ios_base::sync_with_stdio(0);

        cin.tie(0);

        int T;

        cin >> T;

 

        for (int t = 0; t < T; t++)

        {

                 int N;

                 cin >> N;

 

                 for (int i = 1; i <= N; i++)

                 {

                         int num;

                         cin >> num;

 

                         pSum[i] = pSum[i - 1] + num;

                 }

 

                 int result = INT_MIN;

                 for (int i = 1; i <= N; i++)

                         for (int j = i; j <= N; j++)

                                 result = max(result, pSum[j] - pSum[i - 1]);

 

                 cout << result << "\n";

        }

        return 0;

}


개발환경:Visual Studio 2017


지적, 조언, 질문 환영입니다! 댓글 남겨주세요~


반응형