[csp ccf]201909-2小明种苹果(续)

解题思路:

        本题是第一题的难度上升版,但仍然万变不离其宗,只需要针对性的设置多个参数即可

我采用的是每棵树增加一个bool型变量,用来判断是否有苹果掉落。

代码如下:

#include<cstdio>
#include<vector>
using namespace std;

struct tree{
    int first;
    int kill[1001] = {0};
    int killNum = 0;
    int remain; //保存最终的果子数量
    bool isLost = false;
    int lost = 0;
};
int main(){
    int N;
    scanf("%d",&N);
    vector<tree>tlist; //树列表
    for(int i = 0 ; i < N ; i++){
       int m ;
       scanf("%d",&m); //读取mi
       tree temp; //当前果树
       scanf("%d",&temp.first);//第一个数为果树初始数量
       temp.remain = temp.first; //remain保存当前剩余果子
       int k = 0 ; //存储梳果的计数器//类指针
       for(int j = 1 ; j < m ; j++){//将每个数存储进数组中
           int t ;
           scanf("%d",&t);
           //判断数的类型
           //t > 0 为更新果树数量,判断是否有苹果掉落
           if(t > 0){
               if(t < temp.remain){ //若小于当前苹果数量,说明有苹果掉落
                   temp.isLost = true;
                   temp.remain = t;
               }
           }else { //梳果操作
               temp.kill[k] = t; //记录梳果
               k++; //下一次存储在下一个位置
               temp.killNum += t; //梳果总数+
               //更新remain
               temp.remain += t;
           }
       }
       tlist.push_back(temp);
    }//完成输入
    int T = 0 , D = 0 , E = 0;
    //计算T 、 D、 E;
    int Enum = 0;
    for(int i = 0 ; i < N ; i++) {
        T += tlist[i].remain;
        if(tlist[i].isLost == true){
            D++;
        }
        if(tlist[i % N].isLost == true && tlist[(i+1) % N].isLost == true && tlist[(i+2) % N].isLost == true){
            E++;
        }
    }
    //输出
    printf("%d %d %d",T,D,E);
    return 0;
}

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值