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&NBSP;SUBTOTAL:&NBsp {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); &nbSp; console.writeline ("got subtotal: {0}", maxtotalvalue); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LIST&NBSP;=&NBSP;NEW&NBSP;INT[]&NBSP;{&NBSP;22,&NBSP;-33,&NBSP;-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> /// to find out their laws; /// 1. when there is no positive number, you only need to compare a single element, which is the maximum /// 2. when there is a positive number, the, needs to be added, but after each addition, the maximum is marked, and if the sum is 0 or negative, the total is 0; continue to calculate the following. /// time Complexity is 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) {&NBSP;&NBSP;&NBSP;&NBsp; 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; }}}
Find the maximum number of consecutive elements in a given array