科大讯飞笔试
迷惑求解,为什么提交就0%?反正我编的几个测试用例在本地是通过的。。。
礼物最大价值
输入:3 3
2 3 1
1 5 1
4 2 1
1 5 1
4 2 1
输出:13
import java.util.Scanner; public class Solution2 { public static void main(String[] args) { int row = 0,column = 0; Scanner in = new Scanner(System.in); row = in.nextInt(); column= in.nextInt(); int[][] value = new int[row][column]; for(int i = 0;i<row;i++) { for (int j = 0; j < column; j++) { value[i][j] = in.nextInt(); } } System.out.println(giftValue(value)); } public static int giftValue(int[][] value){ int row = value.length; int column = value[0].length; int[][] maxValue = new int[row][column]; /*当前最大价值从左或者上来的,maxValue[i][j] = Max{value[i][j-1],value[i-1][j]}+value[i][j]*/ for(int i = 0;i<row;i++){ for(int j = 0;j<column;j++){ if(i==0&&j==0) maxValue[i][j] = value[i][j]; //只能从左边来 else if(i==0) maxValue[i][j] = maxValue[i][j-1]+value[i][j]; //只能从上面来 else if(j==0) maxValue[i][j] = maxValue[i-1][j]+value[i][j]; else maxValue[i][j] = Math.max(maxValue[i][j-1],maxValue[i-1][j])+value[i][j]; } } return maxValue[row-1][column-1]; } }