CCF CSP模拟题 小明种苹果(续)(C++)

2019-9 小明种苹果(续)

ccf csp刷题记录,方法不一定是最好的,只是多提供一种解题的思路和方法。


题目
在这里插入图片描述
在这里插入图片描述

源代码:

#include<iostream>
using namespace std;

struct aptree {
 int *cutnum;  //new出一个空间来记录每一轮疏掉的苹果数量
 int total;  //该苹果树经过疏果后剩下的果子总量
 int dropapple = 0;
};

int treenums;

int f(int a)  
{
 return a <=treenums ? a : (a - treenums);
}
int main()
{
 cin >> treenums;
 int turns;
 int init, cutnums;
 int totalcut = 0;
 int totalnum = 0;
 aptree *ap;
 ap = new aptree[treenums + 1];
 for (int i = 0;i < treenums;i++)
 {
  cin >> turns;
  cin >> init;
  ap[i].total = init;
  ap[i].cutnum = new int[turns];
  ap[i].dropapple = 0;
  for (int j = 0;j < turns - 1;j++)
  {
   cin >> cutnums;
   if (cutnums <= 0)
   {
    ap[i].cutnum[j] = cutnums;
    totalcut += cutnums;
   }
   if (cutnums > 0)
   {
    if ((ap[i].total + totalcut) <= cutnums)
    {
     ap[i].total = cutnums;
     totalcut = 0;
    }
    else
    {
     ap[i].dropapple = 1;
     ap[i].total = cutnums;
      totalcut = 0;
    }
   }
  }
  ap[i].total += totalcut;
  totalnum += ap[i].total;
  totalcut = 0;
 }
 
 int droptreenums = 0;
 int continuedrop = 0;
 for (int i = 0;i < treenums;i++)
 {
  if (ap[i].dropapple == 1)
  {
   droptreenums++;
  }
  if (ap[i].dropapple == 1 && ap[f(i + 1) % treenums].dropapple == 1 && ap[f(i + 2) % treenums].dropapple == 1)
  {
   continuedrop++;
  }
 }
 cout << totalnum << " " << droptreenums << " " << continuedrop;
  //有new一定要有正确的delete,否则会造成内存泄漏
 for (int i = 1;i <= treenums;i++)
 {
  delete[]ap[i].cutnum;
 }
 delete[]ap;
 return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值