HDU1003

作者: 眼镜猫头鹰 | 来源:发表于2018-12-05 09:18 被阅读2次
    import java.util.Scanner;
    
    /**
     * Created by mac on 2018/11/30.
     */
    public class Main {
        private static final int M = -0x3f3f3f3f;
        public static void main(String[]args){
    //        System.out.println(M);
            Scanner input = new Scanner(System.in);
            int T = input.nextInt();
            for (int i = 1; i <= T; i++){
                int N = input.nextInt();
                //需要判别负数归0后是否会大于最大和,大于则将新的起始值赋予。
                int sum =  0;
                int max_sum = M;
                int M_start = 1;
                int M_end = 1;
                int New_start = 1;
    
                for (int j = 1; j <= N; j++){
                    int n = input.nextInt();
                    sum += n;
                    if (sum > max_sum){
                        max_sum = sum;
                        M_end = j;
                        M_start = New_start;
                    }
                    if (sum < 0){
                        sum = 0;
                        New_start = j + 1;
                    }
                }
                System.out.printf("Case %d:%n", i);
                System.out.printf("%d %d %d%n",max_sum,M_start,M_end);
    //            System.out.println(max_sum + " " + (M_start + 1) + " " + (M_end + 1));
                if (i < T)
                    System.out.println();
            }
        }
    }
    
    
    

    相关文章

      网友评论

        本文标题:HDU1003

        本文链接:https://www.haomeiwen.com/subject/hbxicqtx.html