JAVA CCF-201909-2 小明种苹果(续)

欢迎访问我的CCF认证解题目录

题目描述

思路过程

挺简单的,直接跟着题目说的做就行了

代码

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[] line = br.readLine().split(" ");
		
		int n = Integer.parseInt(line[0]);
		int T = 0, D = 0, E = 0;//对应题目的参数
		boolean[] flag = new boolean[n];//标记是否发生掉落
		
		for ( int i = 0; i < n; i++ ) {
			line = br.readLine().split(" ");
			int m = Integer.parseInt(line[0]);
			int temp = Integer.parseInt(line[1]);//开始的苹果数
			for ( int j = 2; j <= m; j++ ) {
				int num = Integer.parseInt(line[j]);
				if ( num > 0 ) {
					if ( temp != num ) {//发生掉落
						flag[i] = true;
						temp = num;
					}
				} else {
					temp += num;
				}
			}
			T += temp;
		}
		for ( int i = 0; i < n; i++ ) {
			if ( flag[i] ) D++;
			if ( flag[(i+n)%n] && flag[(i+n+1)%n] && flag[(i+n-1)%n] ) {
				E++;
			}
		}
		
		System.out.printf("%d %d %d", T, D, E);
	}
}
发布了60 篇原创文章 · 获赞 0 · 访问量 2146

猜你喜欢

转载自blog.csdn.net/weixin_43732798/article/details/101718201