1003 Brute Force Solution

Source: Internet
Author: User

   /**     * 5 -1 -1 -1 -1 -1      * 7 0 6 -1 1 -6 7 -5      * 5 6 -1 5 4 -7     * g and the largest subsequence, Output Max and start and stop subscript (brute force solution, timeout time limit exceeded)      *  @param  args  the command line arguments     */    public  static void main (String[] args)  {        int  n;        scanner cin = new scanner ( system.in);         n = cin.nextint ();         for (int k =1; k <=n; k++)  {             int[] a = new int[1000000];             int num = cin.nextint ();             a[0] = num;             int length = 1;             while (length <= a[0]) {                   num = cin.nextint ();                 a[length] = num;                 length ++;             }             length = a[0];            int indexbegin = 1;             int indexNext = 1;             int sum = a[1];             for (int j = 1; j  <= length; j++ )  {                 int temp = 0;                 int tempSum = 0;                 for (int i = j; i  <= length; i++)  {                                      temp = tempSum;                     tempSum = tempSum + a[i];                     if (( tempsum >= temp)  &&  (tempsum > sum)) {                          sum = tempSum;                         indexBegin = j;                          indexnext = i;                     }                 }            }             system.out.println ("case "  + k + ': '  ) ;             system.out.println ( sum +   " "  +indexBegin +  " " +indexnext);          }                  }


1003 Brute Force Solution

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.