标签:
给定源数组求该数组中和最大的子数组
遍历源数组,每遍历一次用一个当前子数组和计数器(pseudoResult)将当前遍历过的数字加和,然后判断pseudoResult是否大于0,大于0的话就和最大子数组个计数器(result)比较,取两者较大值作为result的值,然后进行下一次循环;若pseudoResult小于等于0,pseudoResult的值置为0(意味着舍去之前计算的数组结果重新计算),然后进行下一次循环。
https://coding.net/u/qianfeiqianlan/p/software_engineering_code/git
(SumMaximumSubArray为最大子数组求和,SumMaximumSubArrayTest为测试项目)
(添加后测试项目中的引用结构如图所示)
1 using System; 2 using Microsoft.VisualStudio.TestTools.UnitTesting; 3 using SumMaximumSubArrayQuestion; 4 5 namespace SumMaximumSubArrayTest 6 { 7 [TestClass] 8 public class UnitTest1 9 { 10 [TestMethod] 11 public void SumMaximumSubArrayTest1() 12 { 13 int[] firstArray = { 1, -2, 3, -4, 5, 6, 0 }; 14 Assert.AreEqual(Program.SumMaximumSubArray(firstArray), 11); 15 } 16 17 [TestMethod] 18 public void SumMaximumSubArrayTest2() 19 { 20 int[] secondArray = { 1, 2, 3, 4, -1, -7, 9 }; 21 Assert.AreEqual(Program.SumMaximumSubArray(secondArray), 11); 22 } 23 24 [TestMethod] 25 public void SumMaximumSubArrayTest3() 26 { 27 int[] thirdArray = { 111, 20, 30, 44, -4, -7, 19 }; 28 Assert.AreEqual(Program.SumMaximumSubArray(thirdArray), 213); 29 30 } 31 32 [TestMethod] 33 public void SumMaximumSubArrayTest4() 34 { 35 int[] fourthArray = { -4, 4, -2, 9, 7, -4, 1, 0, -3, 4 }; 36 Assert.AreEqual(Program.SumMaximumSubArray(fourthArray), 18); 37 38 } 39 40 [TestMethod] 41 public void SumMaximumSubArrayTest5() 42 { 43 int[] fifthArray = { 1, 12, -3, 4, -1, -7, 9 }; 44 Assert.AreEqual(Program.SumMaximumSubArray(fifthArray), 15); 45 46 } 47 } 48 }
用例编号 | 用例描述 |
输入数据 |
预期输出数据 | 实际输出数据 | 通过/不通过 | 评价 |
1 |
1, -2, 3, -4, 5, 6, 0 |
11 | 11 |
true |
||
2 |
1, 2, 3, 4, -1, -7, 9 |
11 | 11 | true | ||
3 |
111, 20, 30, 44, -4, -7, 19 |
213 | 213 | true | ||
4 |
-4, 4, -2, 9, 7, -4, 1, 0, -3, 4 |
18 | 18 | true | ||
5 |
1, 12, -3, 4, -1, -7, 9 |
15 | 15 | true |
http://blog.csdn.net/williamwang2013/article/details/8643714
标签:
原文地址:http://www.cnblogs.com/qianfeiqianlan/p/5944557.html