【CCF历年题题解】201909-2 小明种苹果(续)

同上模拟,注意掉落的树不可重复统计

#include <iostream>

using namespace std;

const int N = 1010;

int n,m;
int a[N]; // 苹果的数量
bool st[N]; // 苹果是否掉落

int main()
{
    
    
    int d = 0; 
    cin >> n;
    for(int i=1;i<=n;i++)
    {
    
    
        cin >> m;
        int flag = 1; // 每一棵树的d不可重复统计,hack数据:14 20 19 18 17
        for(int j=1;j<=m;j++)
        {
    
    
            int x;
            cin >> x;
            if(j == 1) a[i] = x;
            else if(x > 0) 
            {
    
    
                if(a[i] != x){
    
    
                  a[i] = x, st[i] = true;
                  if(flag) d ++ ,flag = 0; // 发生掉落  ,不重复统计
                } 
            }
            else a[i] += x;
        }
    }
    
    int cnt = 0;
    for(int i=1;i<=n;i++) cnt += a[i];
    cout<<cnt<<" "<<d<<" ";
    
    int drop = 0; // 掉落的次数,drop 就是e

    for(int i=1;i<=n;i++)
    {
    
    
        if(i == 1) {
    
     // 打括号不容易出错
            if(st[i] && st[i+1] && st[n]) drop ++;
        }
        else if(i == n) {
    
    
            if(st[i - 1] && st[i] && st[1]) drop ++;
        }
        else 
        {
    
    
            if(st[i-1] && st[i] && st[i + 1]) drop ++;
        }
            
    }
    
    cout<<drop<<endl;
    
    //for(int i=1;i<=n;i++) cout<<st[i]<<" ";
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43154149/article/details/107962563
今日推荐