20190902-2 小明种苹果(续)

20190902 小明种苹果(续)

思路:

在遍历记录是否掉苹果的数组drop,i从编号1到n。如果drop[i]==true,则这颗树存在掉落;如果drop[i]&&drop[(i+1)%n]&&drop[(i+2)%n]==true,则存在一组相邻连续三棵树发生掉落;

坑:

掉落多次苹果时不要重复计数!!!!

#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int main()
{
	vector<int>tree;//每个树上的苹果数
	vector<int>isFall;//每个树是否掉落果子
	int n = 0;//计数循环变量
	int fall = 0;//掉落苹果的苹果树数量
	int N; cin >> N;
	while (n<N)
	{
		int times; cin >> times;//每个树的操作次数
		int originapple; cin >> originapple; //最开始树上的苹果数
		tree.push_back(originapple);
		isFall.push_back(0);
		for (int i = 1; i < times; i++)//N-1次输入
		{
			int check; cin >> check;
			if (check <= 0)//小明疏果
			{
				tree[n] += check;
			}
			else //苹果自己掉落
			{
				if (check < tree[n])
				{
					isFall[n] = 1;
					tree[n] = check;
					//fall++;//掉落多次苹果时重复计数!!!!
				}
			}
		}
		n++;
	}
	
	int total = 0;//所有树上苹果总数
	for (int i = 0; i < N; i++)
		total += tree[i];
	cout << total << " ";

	int cnt = 0;//相邻三棵树同时掉落苹果的组数		
	for (int i = 0; i < N; i++)
		if (isFall[i] == 1)
		{
			fall++;
			if (isFall[(i + 1) % N] == 1 && isFall[(i + 2) % N] == 1)
				cnt++;
		}
	if (N <= 2)
		cnt = 0;
	cout << fall << " " << cnt << endl;
	return 0;
}

数据

4
4 74 -7 -12 -5
5 73 -8 -6 59 -4
5 76 -5 -10 60 -2
5 80 -6 -15 59 0

5
4 10 0 9 0
4 10 -2 7 0
2 10 0
4 10 -3 5 0
4 10 -1 8 0

1
4 20 19 18 17

猜你喜欢

转载自blog.csdn.net/helloworld0529/article/details/107759924