From d53595640a11870171fdc6ae4173c25d2b56215d Mon Sep 17 00:00:00 2001 From: hao14293 Date: Wed, 1 May 2019 13:39:21 +0800 Subject: [PATCH] =?UTF-8?q?Create=2001-=E5=A4=8D=E6=9D=82=E5=BA=A62=20Maxi?= =?UTF-8?q?mum=20Subsequence=20Sum=20(25=20=E5=88=86).cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...mum Subsequence Sum (25 \345\210\206).cpp" | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 "ZJU_MOOC_\346\225\260\346\215\256\347\273\223\346\236\204/01-\345\244\215\346\235\202\345\272\2462 Maximum Subsequence Sum (25 \345\210\206).cpp" diff --git "a/ZJU_MOOC_\346\225\260\346\215\256\347\273\223\346\236\204/01-\345\244\215\346\235\202\345\272\2462 Maximum Subsequence Sum (25 \345\210\206).cpp" "b/ZJU_MOOC_\346\225\260\346\215\256\347\273\223\346\236\204/01-\345\244\215\346\235\202\345\272\2462 Maximum Subsequence Sum (25 \345\210\206).cpp" new file mode 100644 index 0000000..c734994 --- /dev/null +++ "b/ZJU_MOOC_\346\225\260\346\215\256\347\273\223\346\236\204/01-\345\244\215\346\235\202\345\272\2462 Maximum Subsequence Sum (25 \345\210\206).cpp" @@ -0,0 +1,24 @@ +#include +#include +using namespace std; +int main() { + int n; + scanf("%d", &n); + vector v(n); + int leftindex = 0, rightindex = n - 1, sum = -1, temp = 0, tempindex = 0; + for (int i = 0; i < n; i++) { + scanf("%d", &v[i]); + temp = temp + v[i]; + if (temp < 0) { + temp = 0; + tempindex = i + 1; + } else if (temp > sum) { + sum = temp; + leftindex = tempindex; + rightindex = i; + } + } + if (sum < 0) sum = 0; + printf("%d %d %d", sum, v[leftindex], v[rightindex]); + return 0; +}