小明种苹果
package practice;
import java.awt.print.Printable;
import java.util.Scanner;
public class test19091 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();//苹果树个数
int M = sc.nextInt();//蔬果操作轮数
int[] nums = new int[N];//存储每一棵树上最后剩下的苹果
int[] a = new int[N];//存储每棵树掉的果子
int b = 0;
for (int i = 0; i < N; i++) {//每次读一行
int total = sc.nextInt();//未修建时对应苹果树上的苹果个数
for (int j = 0; j < M; j++) {//每修建一次
a[i] += Math.abs(sc.nextInt());
}
nums[i] +=total-a[i];
}
for (int i = 0; i < nums.length; i++) {
b +=nums[i];
}
System.out.print(b+" ");
int t = 0;
int max = a[0];
for (int i = 0; i < a.length; i++) {//计算最多修建果树
if (a[i]>max) {
max = a[i];
t = i;
}
}
System.out.print(t+1+" ");
System.out.print(max);
}
}
总结
一个比较简单的问题,就是每次计算实时的数据,实时的数据不用存储。若不是实时的数据,考虑使用数组,二维数组,字符串,类加集合存储,以后写代码,最好每一步加注解。便于后面的改动。