求大佬看下8.14阿里面试第一题为什么我只能过20%。。。
看了下同学的python代码感觉思路都一样啊。。
代码如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String nd = in.nextLine();
String[] ndStrs = nd.split(" ");
int n = Integer.parseInt(ndStrs[0]);
long d = Integer.parseInt(ndStrs[1]);
long[][] nn = new long[n][n];
long maxVal = Integer.MIN_VALUE;
for(int i = 0; i < n; i++) {
String nLine = in.nextLine();
String[] split = nLine.split(" ");
for (int j = 0;i<split.length;i++) {
nn[i][j] = Integer.parseInt(split[i]);
if(nn[i][j] > maxVal) {
maxVal = nn[i][j];
}
}
}
long step = 0;
boolean flag = false;
for(int i = 0;i< nn[0].length;i++) {
for(int j = 0;j < nn[0].length;j++) {
if((maxVal - nn[i][j]) % d == 0) {
step += (maxVal - nn[i][j]) / d;
} else {
flag = true;
break;
}
}
if(flag) break;
}
if(flag) {
System.out.println(-1);
} else {
System.out.println(step);
}
}
}
#阿里巴巴##笔试题目#public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String nd = in.nextLine();
String[] ndStrs = nd.split(" ");
int n = Integer.parseInt(ndStrs[0]);
long d = Integer.parseInt(ndStrs[1]);
long[][] nn = new long[n][n];
long maxVal = Integer.MIN_VALUE;
for(int i = 0; i < n; i++) {
String nLine = in.nextLine();
String[] split = nLine.split(" ");
for (int j = 0;i<split.length;i++) {
nn[i][j] = Integer.parseInt(split[i]);
if(nn[i][j] > maxVal) {
maxVal = nn[i][j];
}
}
}
long step = 0;
boolean flag = false;
for(int i = 0;i< nn[0].length;i++) {
for(int j = 0;j < nn[0].length;j++) {
if((maxVal - nn[i][j]) % d == 0) {
step += (maxVal - nn[i][j]) / d;
} else {
flag = true;
break;
}
}
if(flag) break;
}
if(flag) {
System.out.println(-1);
} else {
System.out.println(step);
}
}
}