9 degree OJ question: Maximum subvector and

Source: Internet
Author: User

Time Limit: 1 second memory limit: 32 MB Special Judgment: No submitted: 396 solution: 109 question Description: HZ occasionally takes some professional questions to fool those non-computer professional students. Today, after the JOBDU test group was opened, he spoke again: in the Ancient One-dimensional pattern recognition, it is often necessary to calculate the largest sum of continuous subvectors. When all vectors are positive, the problem is well solved. However, if a vector contains a negative number, should it contain a negative number and expect the positive number next to it to compensate? For example: {6,-3,-0th,-15, 1, 3rd}, the maximum sum of continuous subvectors is 8 (starting from ). Will you be fooled by him? Input: multiple groups of data are input. Each group of test data includes two rows. The first act is an integer n (0 <= n <= 100000). When n = 0, the input ends. The next row contains n integers (we guarantee that all integers belong to [-]). Output: For each test case, three integers must be output in a single row, represents the largest continuous subvector and the subscript of the first element of the subvector and the subscript of the last element, respectively. If multiple subvectors exist, the smallest starting element is output. Sample input: 3-1-3-25-8 3 2 0 586-3-2 7-15 1 2 20 sample output: -1 0 010 1 48 0 3 [cpp]/*************************** * ***** Date: * Author: SJF0115 * question: jiudu OJ Question 15: largest subvector and * result: AC * Source: offoffer: famous Enterprise interviewers explain typical programming issues * Summary ******************************** **/# include <stdio. h> int Num [100001]; int main () {int I, j, n, sum, Max, indexStart, indexEnd, currentStart; while (scanf ("% d ", & n )! = EOF & n! = 0) {sum = 0; for (I = 0; I <n; I ++) {scanf ("% d", & Num [I]); // initialize if (I = 0) {Max = Num [I]; currentStart = 0; indexStart = 0; indexEnd = 0 ;} // if the sum currently obtained is a negative number, the sum should be discarded and reset in the following tired addition, otherwise, this negative number will decrease the following sum (sum <0) {sum = Num [I]; currentStart = I;} else {sum + = Num [I];} if (sum> Max) {Max = sum; indexStart = currentStart; indexEnd = I ;}// Note: if multiple subvectors exist, then the else if (sum = Max) {if (Num [indexStart]> Num [currentStart]) {indexStart = currentStart; indexEnd = I ;}} with the smallest starting element is output ;}}} printf ("% d \ n", Max, indexStart, indexEnd);} // while return 0;

Related Article

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.