问题链接:小明种苹果(续)问题分析:模拟法解决。程序说明:n表示苹果树棵树;m表示操作记录次数;count表示苹果树苹果数量(第一次操作记录赋值);sub表示第一次检查苹果树之后的记录;drop数组记录掉落情况;total为剩下的苹果总数,tree为掉落的棵树,group为掉落的组数程序代码: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner reader = new Scanner(System.in); int n = reader.nextInt(); int total = 0; boolean []drop = new boolean[n]; for(int i=0;i<n;i++) { int m = reader.nextInt(); int count=0; for(int j=0;j<m;j++) { if(j==0) { count=reader.nextInt(); } else { int sub = reader.nextInt(); if(sub<=0) { count+=sub; } else { if(count>sub) { count=sub; drop[i]=true; } } } } total+=count; } int group=0; int tree=0; for(int i=0;i<n;i++) { if(drop[i]) { tree++; } if(i==0) { if(drop[i]&&drop[i+1]&&drop[n-1]) group++; } else { if(drop[i]&&drop[(i+1)%n]&&drop[i-1]) group++; } } System.out.println(total+" "+tree+" "+group); } }