<font color=666666 size=4>流程圖</font> ios
<font color=666666 size=4>源代碼</font> </font><font color=pink size=4>點此查看Github源代碼</font>git
#include <stdio.h> #include<iostream> #include<cstdlib> using namespace std; int MAX(int n,int *a) { int m, max1=0,sub=0; for (m = 0; m < n; m++) { sub += a[m]; if(sub<a[m]) { sub = a[m]; } if (max1 < sub) { max1 = sub; } } return max1; } int main() { int i=0,max=0,n=0,button=0; int* a = new int[n+1]; cin >> n; if (n <= 0) { cout << max; system("pause"); return 0; } return 0; for (i = 0; i < n; i++) { cin >> a[i]; if (a[i] > 0) { button = 1; } } if (button == 0) { max = 0; } else { max = MAX(n,a); } cout << max << endl; system("pause"); return 0; }
\ | A | B | C | D |
---|---|---|---|---|
1 | n<=0 | a[0]_a[n]<0 | sub<a[i] | max1<sub |
2 | n>0 | a[i]>0 | sub>=a[I] | max1>=sub |
n=0;A1 | ||||
n=6,a[]={-2,11,-4,13,-5,-2};A2,B2,C1,C2,D1,D2 | ||||
n=6,a[]={-2,-11,-4,-13,-5,-2};A2,B1 | ||||
<font color=gray size=4></font> | ||||
<font color=666666 size=4>單元測試代碼</font> |
#include "stdafx.h" #include "CppUnitTest.h" #include "../2/head.h" using namespace Microsoft::VisualStudio::CppUnitTestFramework; namespace UnitTest1 { TEST_CLASS(UnitTest1) { public: TEST_METHOD(TestMethod1) { int n = 6; int num[] = { -2,11,-4,13,-5,-2 }; Assert::AreEqual(MAX(n, num), 20); } TEST_METHOD(TestMethod2) { int n = 6; int num[] = { -2,-11,-4,-13,-5,-2 }; Assert::AreEqual(MAX(n, num), 0); } TEST_METHOD(TestMethod3) { int n = -1; int num[] = { -2,11,-4,13,-5,-2 }; Assert::AreEqual(MAX(n, num), 0); } }; }
<font color=666666 size=4>測試結果圖</font> github