1007. Maximum subsequence sum (25) C # implementation

Source: Internet
Author: User

The Code is as follows:

1 public static int f = 0, L = 0; 2 3 static void main (string [] ARGs) 4 {5 Int first = 0; // when the current maximum value is obtained, corresponding to the first number 6 int last = 0; // when the current maximum value is obtained, the last number 7 int p = 0; // the current maximum value is 8 int K; 9 int [] n; // store K Consecutive Integers 10 11 K = convert. toint32 (console. readline (); 12 N = new int [k]; 13 string [] STR = console. readline (). split (New char [] {''}); 14 for (INT I = 0; I <K; I ++) 15 {16 N [I] = int. parse (STR [I]); // storage value 17} 18 19 // initialize 20 p = maxnum (k, 1, n); 21 first = F; 22 last = L; 23 24 for (INT I = 2; I <= K; I ++) 25 {26 if (maxnum (K, I, n)> P) 27 {28 p = maxnum (K, I, n); 29 First = f; 30 last = L; 31} 32 else if (maxnum (K, I, n) = P & first> F) // The maximum value is the same, but the minimum index is different. 33 {34 p = maxnum (K, I, n); 35 first = F; 36 last = L; 37} 38} 39 40 console. write (p + "" + first + "" + last); 41 console. readkey (); 42 43} 44 45 // when the group is continuous, its maximum value is 46 // and its minimum value is 47 Public static int maxnum (int K, int group, int [] n) 48 {49 int max = 0; 50 Int J, I; 51 int temp; 52 for (I = 0; I <= k-group; I ++) 53 {54 temp = 0; 55 56 for (j = 0; j <group; j ++) 57 {58 temp + = N [I + J]; 59} 60 61 If (temp> MAX) 62 {63 max = temp; 64 f = N [I]; // set the global variable 65 L = N [I + J-1]; // set the global variable 66} 67} 68 69 If (max <0 & Group = 1) // when all numbers are negative, set Value 70 {71 F = N [0]; 72 L = N [K-1]; 73 return 0; 74} 75 else76 return Max; 77 78}

 

1007. Maximum subsequence sum (25) C # implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.