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

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

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
30分 例子能过

#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
int main(void){
    int i = 0,n,m,ans = 0,ansi = 0,h,D = 0,k = 0,ansj = 0;
    int a[5] = {0};
    scanf("%d",&n);bool b[n] = {false};
    while(n--){
        ansi = 0;
        scanf("%d", &m);
        for(i = 0;i < m; i++){
            scanf("%d", &h);
            if(h > 0){
                if(i==0){
                    ansi = h;
                }
                else if(ansi != h){
                    D++;ansi = h;b[n] = true;
                }
            }
            else ansi += h;
        }
        ans += ansi;
    }
    for(i = 0;i < n-2; i++){
        if(b[i]&&b[i+1]&&b[i+2]) ansj++;
    }
    if(b[n-2]&&b[n-1]&&b[0]) ansj++;
    if(b[n-1]&&b[0]&&b[1]) ansj++;
    printf("%d %d %d\n", ans, D, ansj);
    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

*/

n的问题,前面n–,后面检查连续用的还是n。
假如现在计算得出的果树数,和后面重新统计输入的正数 值相等,就不要再ansi+=h了,所以加了if(h<=0)限制。
50分

#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
int main(void){
    int i,n,m,ans = 0,ansi = 0,h,D = 0,k ,ansj = 0;
    scanf("%d",&n);int b[n] = {0};k=n;
    while(n--){
        scanf("%d%d", &m,&ansi);
        for(i = 1;i < m; i++){
            scanf("%d", &h);
            if(h > 0&&ansi != h){
                D++;ansi = h;b[n] = 1;
            }
            else if(h<=0) ansi += h;
        }
        ans += ansi;
    }n=k;
    for(i = 0;i < n-2; i++){
        if(b[i]&&b[i+1]&&b[i+2]) ansj++;
    }
    if(b[n-2]&&b[n-1]&&b[0]) ansj++;
    if(b[n-1]&&b[0]&&b[1]) ansj++;
    printf("%d %d %d\n", ans, D, ansj);
    return 0;
}

如果某棵树被记录为掉落的树,那么记录一次即可, 不能多次记录。否则就会出错,只得了50分,就是没有考虑到这点。加入flag用来记录,这棵树是否已经被记录过了。
100分

#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
int main(void){
    int i,n,m,T = 0,h,D = 0,E = 0,a;
    scanf("%d",&n);int b[n+2] = {0};
    for(int j=0;j<n;j++){
        bool flag=true;
        scanf("%d%d", &m,&a);
        for(i = 1;i < m; i++){
            scanf("%d", &h);
            if(h > 0&&h != a){
                    if(flag){
                        b[j] = 1;D++;
                    }
                a = h;flag=false;
            }
            else if(h<=0) a += h;
        }
        T += a;
    }
    for(i = 0;i < n-2; i++){
        if(b[i]&&b[i+1]&&b[i+2]) E++;
    }
    if(b[n-2]&&b[n-1]&&b[0]) E++;
    if(b[n-1]&&b[0]&&b[1]) E++;
    printf("%d %d %d", T, D, E);
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值