代码1
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int t = 0, k = 0, p = 0;
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int[][] a = new int[n][m+1];
int[] b = new int[n]; // 记录每棵树的疏果个数
for(int i = 0; i < n; i++) {
for(int j = 0; j < m+1; j++) {
a[i][j] = in.nextInt();
b[i] += Math.abs(a[i][j]);
t += a[i][j];
}
b[i] -= a[i][0]; // 第i棵树的疏果个数
}
in.close();
for(int i = 1; i < n; i++) {
if(b[i] > b[k]) {
k = i;
}
}
p = b[k];
System.out.print(t + " " + (k+1) + " " + p);
}
}
代码2
使用BufferedReader进入输入,耗费的时间和空间都比Scanner类要少。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] s = br.readLine().split(" ");
int n = Integer.parseInt(s[0]);
int m = Integer.parseInt(s[1]);
int t = 0, k = 0, p = 0;
int[][] a = new int[n][m+1];
int[] b = new int[n]; // 记录每棵苹果树的蔬果个数
for(int i = 0; i < n; i++){
s = br.readLine().split(" ");
for(int j = 0; j < m+1; j++){
a[i][j] = Integer.parseInt(s[j]);
t += a[i][j];
b[i] += Math.abs(a[i][j]);
}
b[i] -= a[i][0];
}
br.close();
for(int i = 1; i < n; i++){
if(b[i] > b[k]){
k = i;
}
}
p = b[k];
System.out.printf("%d %d %d", t, k+1, p);
}
}