CCF-CSP 201909-2 Xiaoming Apple (continued)

Insert picture description here
Insert picture description here
Insert picture description here

Problem-solving ideas:
use an array of length n + 2 to record whether a tree has dropped apples. The reason for adding 2 more than n is because of the simulation loop.
That is: drops [0] = drops [n]; drops [1] = drops [n + 1]; the first
two and the last two are equal to simulate the ring.

Summary of experience:
If a tree is recorded as a fallen tree, it can be recorded only once, not multiple times. Otherwise, you will make mistakes. You only got 50 points in the exam, but you did not take this into account.

C ++ code:

#include<bits/stdc++.h>
using namespace std;
int main() {
	int n,m,D = 0,E = 0,T = 0;
	scanf("%d",&n);
	int drops[n+2] = {0};
	for(int i=0; i<n; i++) {
		int a,b;
		bool flag_drop = true;
		scanf("%d %d",&m,&a);
		for(int j=1; j<m; j++) {
			scanf("%d",&b);
			if(b>0&&a!=b) {
				if(flag_drop) { //注意这里一定要加个判断,某棵树掉落一次统计一次即可,多次统计会出现错误
					drops[i+2] = 1;
					D++; //如果多次统计掉落,D值会变大
				}
				a = b;
				flag_drop = false;
			} else if(b<=0) {
				a += b;
			}
		}
		T += a;
	}
	drops[0]=drops[n];
	drops[1]=drops[n+1];
	for(int i=1; i<n+1; i++) {
		if(1 == drops[i-1] && 1 == drops[i] && 1 == drops[i+1]) {
			E++;
		}
	}
	printf("%d %d %d",T,D,E);
	return 0;
}
Published 111 original articles · won praise 2 · Views 3533

Guess you like

Origin blog.csdn.net/m0_38088647/article/details/101393686