码迷,mamicode.com
首页 > 其他好文 > 详细

1003蛮力解法

时间:2016-07-05 06:35:34      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:java

还是1003的问题蛮力解法,会超时:

   /**
     * 5 -1 -1 -1 -1 -1
     * 7 0 6 -1 1 -6 7 -5
     * 5 6 -1 5 4 -7
     * G和最大的子序列,输出最大和还有起始与截止下标(暴力解法,超时了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蛮力解法

标签:java

原文地址:http://11543608.blog.51cto.com/11533608/1795736

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!