您好,登錄后才能下訂單哦!
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; } } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。