First, solve the problem. Then, write the code. ” - John Johnson

It's not at all important to get it right the first time. It's vitally important to get it right the last time. ” - Andrew Hunt and David Thomas

Tuesday, July 15, 2014

Maximum Sum

Author : Jisan
Programming language : Java
Difficulty : Easy
Status : Accepted
Problem number : UVA-108
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=44

Help: http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html

Code:

import java.util.Scanner;
public class Uva_108 {
    public static void main(String[] args){
       Scanner in=new Scanner(System.in);
       int number=in.nextInt();
       int [][] tomato=new int[number][number];
       for(int a=0;a<tomato.length;a++){
      for(int b=0;b<tomato.length;b++){
      tomato[a][b]=in.nextInt();
      }
       }
       int FSum=Integer.MIN_VALUE;
       for(int p=0;p<number;p++){
      int[] sum=new int[number];
      for(int q=p;q<number;q++){
      int max=Integer.MIN_VALUE;
      int tempSum=0;
      for(int i=0;i<number;i++){
      sum[i]+=tomato[q][i];
      tempSum+=sum[i];
      max=Math.max(max,tempSum);
      if(tempSum<0){
      tempSum=0;
      }
      }
      FSum=Math.max(max, FSum);
      }
       }
       System.out.println(FSum);
    }
}

No comments:

Post a Comment