阿里笔试0325
第二题 过了90%
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int q = sc.nextInt(); sc.nextLine(); String[][] numStr=new String[n][m]; for(int i=0;i<n;i++){ numStr[i]=sc.nextLine().split(" "); } String[][] info=new String[q][2]; for(int i=0;i<q;i++){ info[i]=sc.nextLine().split(" "); } int[][] nums=new int[n][m]; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ nums[i][j]=Integer.parseInt(numStr[i][j]); } } Integer[] row= new Integer[n]; Integer[] col=new Integer[m]; Boolean flag=true; while(flag) { for (int i = 0; i < n; i++) { int idx = -1; int value = 0; for (int j = 0; j < m; j++) { if (nums[i][j] != 0 && idx == -1) { idx = j; value = nums[i][j]; } else if (nums[i][j] != 0) { row[i] = (nums[i][j] - value) / (j - idx); if (nums[i][0] == 0) { nums[i][0] = value - idx * row[i]; } break; } } } for (int j = 0; j < m; j++) { int idx = -1; int value = 0; for (int i = 0; i < n; i++) { if (nums[i][j] != 0 && idx == -1) { idx = i; value = nums[i][j]; } else if (nums[i][j] != 0) { col[j] = (nums[i][j] - value) / (i - idx); if (nums[0][j] == 0) { nums[0][j] = value - idx * col[j]; } break; } } } int count=0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (nums[i][j] == 0 && (row[i] != null || col[j] != null)) { nums[i][j] = row[i] == null ? nums[0][j] + i * col[j] : nums[i][0] + j * row[i]; count+=1; } } } if(count==0) flag=false; } for(int i=0;i<q;i++){ int r=Integer.parseInt(info[i][0])-1; int c=Integer.parseInt(info[i][1])-1; if(nums[r][c]==0) System.out.println("Unknown"); else System.out.println(nums[r][c]); } } }