using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication4 { class Program { static void Main(string[] args) { FindMaxAmountValue rr = new FindMaxAmountValue(); // Random r = new Random(); int[] list=new int[]{-22,-11, 0, 0,0,0}; for (int i = 0; i < list.Length; i++) { Console.Write(list[i] + " "); } int maxTotalValue=0; rr.FindMaxA(list,out maxTotalValue); Console.WriteLine("got subTotal: {0}", maxTotalValue); list = new int[] {0, 0, 0, 0 }; for (int i = 0; i < list.Length; i++) { Console.Write(list[i] + " "); } maxTotalValue = 0; rr.FindMaxA(list, out maxTotalValue); Console.WriteLine("got subTotal: {0}", maxTotalValue); list = new int[] { -22, -33, -1, -10 }; for (int i = 0; i < list.Length; i++) { Console.Write(list[i] + " "); } maxTotalValue = 0; rr.FindMaxA(list, out maxTotalValue); Console.WriteLine("got subTotal: {0}", maxTotalValue); list = new int[] { 22, -33, -100, -10 }; for (int i = 0; i < list.Length; i++) { Console.Write(list[i] + " "); } maxTotalValue = 0; rr.FindMaxA(list, out maxTotalValue); Console.WriteLine("got subTotal: {0}", maxTotalValue); list = new int[] { 22, -33, -100, -10, 19, 18, 13, 25, 21 }; for (int i = 0; i < list.Length; i++) { Console.Write(list[i] + " "); } maxTotalValue = 0; rr.FindMaxA(list, out maxTotalValue); Console.WriteLine("got subTotal: {0}", maxTotalValue); list = new int[] { -22, -33, -100, -10, 19, 18, 13, 25, 21 }; for (int i = 0; i < list.Length; i++) { Console.Write(list[i] + " "); } maxTotalValue = 0; rr.FindMaxA(list, out maxTotalValue); Console.WriteLine("got subTotal: {0}", maxTotalValue); list = new int[] { 22, -33, -100, -10, 19, 18, 13, 25, 21, -21 }; for (int i = 0; i < list.Length; i++) { Console.Write(list[i] + " "); } maxTotalValue = 0; rr.FindMaxA(list, out maxTotalValue); Console.WriteLine("got subTotal: {0}", maxTotalValue); } } public class FindMaxAmountValue { /// <summary> /// 找出它们的规律; /// 1. 当没有正数时,只需要比较单个元素,它就是最大值 /// 2. 当有正数时, 需要相加,但是每次加后,标记出最大值;并且若和为0或者负数时,总和清零;继续计算后面的。 /// 时间复杂度是Q(n)。 /// </summary> /// <param name="list"></param> /// <param name="maxTotalValue"></param> /// <returns>true, that means, that is max sum, otherwise, no max sum value.</returns> public bool FindMaxA(int[] list, out int maxTotalValue) { maxTotalValue = Int32.MinValue; int len = list.Length; if (list == null || len <= 0) return false; // not can‘t find its maximum value. //1. check if none of them in the list are positive -------------- int i = 0; for (; i < len;i++ ) { if (list[i] > 0) { break; } else { if (list[i] > maxTotalValue) { maxTotalValue = list[i]; } } } // there is not any positive number in the list, return it. if (i == len) { return true; } // 2. There are positive number in the list, handle it -------------------------- // we know the list[i] is greater than 0. int currentMaxTotalValue = 0; for (; i < len;i++ ) { currentMaxTotalValue = currentMaxTotalValue + list[i]; if(currentMaxTotalValue>maxTotalValue) { maxTotalValue = currentMaxTotalValue; } if (currentMaxTotalValue < 0) { currentMaxTotalValue = 0; } } return true; } } }
时间: 2024-11-08 06:53:13