1.题目要求

-- 引用自《百度百科》

2.实现代码


#include <iostream>
using namespace std;

int sum(int a[] ,int count)
{
int b[100];
int i;
int max;
b[0] = a[0];
max = b[0];
for (i = 1; i < count; i++)
{
if (b[i - 1] > 0)
b[i] = b[i - 1] + a[i];
else
b[i] = a[i];
if (b[i] > max)
max = b[i];
}
return max;
}

int main()
{
int count;
int a[100];
int i;
int max;
cin >>count;
for (i = 0; i < count; i++)
{
cin >> a[i];
}
max = sum(a, count);
cout << max;
return 0;
}


3.测试代码

b[i-1]>0,b[i]>max abdef
b[i-1]<=0,b[i]>max acdef
b[i-1]>0,b[i]<=max abdf
b[i-1]<=0,b[i]<=max acdf

a[]={1,5,9},max=15
a[]={-1,5,-1},max=5
a[]={-8,-2,-5,8},max=8
a[]={ -2,11,-4,13,-5,-2},max=20

            TEST_METHOD(TestMethod1)
{
int max, num[3] = { 1,5,9 };
max = sum(num, 3);
Assert::AreEqual(max, 15);
}

TEST_METHOD(TestMethod2)
{
int max, num[3] = { -1,5,-1 };
max = sum(num, 3);
Assert::AreEqual(max, 5);
}
TEST_METHOD(TestMethod3)
{
int max, num[4] = { -8,-2,-5,8 };
max = sum(num, 4);
Assert::AreEqual(max, 8);
}
TEST_METHOD(TestMethod4)
{
int max, num[6] = { -2,11,-4,13,-5,-2 };
max = sum(num, 6);
Assert::AreEqual(max, 20);
}


4.测试结果

posted on 2019-04-21 21:36  6886641  阅读(102)  评论(0编辑  收藏  举报