标签:
1 package javaTest; 2 3 import java.util.Scanner; 4 5 public class test { 6 7 public static void main(String[] args){ 8 // TODO Auto-generated method stub 9 Scanner scanner = new Scanner(System.in); 10 String inputString = scanner.nextLine(); 11 12 //get matrix 13 int[][] matrix = getMatrix(inputString); 14 int[] rowcol=getXY(matrix); 15 int row=rowcol[0]; 16 int col=rowcol[1]; 17 try { 18 System.out.print(matrix[row][col]+matrix[row][col+1]+matrix[row+1][col]+matrix[row+1][col+1]); 19 } catch (Exception e) { 20 // TODO: handle exception 21 } 22 scanner.close(); 23 } 24 //根据输入的字符串得到新字符 25 static int[][] getMatrix(String str){ 26 int[][] matrix; 27 String[] lineStrArray = str.split(";"); 28 String[] charArrayStrings = lineStrArray[0].split(" "); 29 30 int row = lineStrArray.length; 31 int col = charArrayStrings.length; 32 matrix = new int[row][col]; 33 for(int i=0;i<row;i++) 34 for(int j=0;j<col;j++){ 35 String[] ArrayStrings = lineStrArray[i].split(" "); 36 try{ 37 matrix[i][j]=Integer.parseInt(ArrayStrings[j]); 38 } 39 catch(Exception e){ 40 41 } 42 } 43 return matrix; 44 } 45 46 static int[] getXY(int[][] matrix){ 47 int row = matrix.length; 48 int col = matrix[0].length; 49 //get results 50 int[][] result=new int[row-1][col-1]; 51 for(int i=0,k=0;i<row-1;i++){ 52 for(int j=0;j<col-1;j++){ 53 int r=matrix[i][j]+matrix[i][j+1]+matrix[i+1][j]+matrix[i+1][j+1]; 54 result[i][j]=r; 55 } 56 } 57 int[] index = getMaxIndex(result); 58 59 int[] rowcol=new int[2]; 60 rowcol[0] = index[0]; 61 rowcol[1] = index[1]; 62 return rowcol; 63 } 64 65 static int[] getMaxIndex(int[][] result){ 66 int[] rowcol=new int[2]; 67 int max=result[0][0]; 68 int maxI=0,maxJ=0; 69 for(int i=1;i<result.length;i++){ 70 for(int j=0;j<result[0].length;j++){ 71 if(max<result[i][j]){ 72 max = result[i][j]; 73 maxI=i; 74 maxJ=j; 75 } 76 } 77 } 78 rowcol[0]=maxI; 79 rowcol[1]=maxJ; 80 return rowcol; 81 } 82 }
标签:
原文地址:http://www.cnblogs.com/kunjie/p/4839624.html