CF探险(20201224)

CF探险(20201224)


前言

先从简单题目入手,慢慢学习。

一、题目

在这里插入图片描述

二、代码

1.io

Example
Input
7
11
3 1 4 1 5 9 2 6 5 3 5
1
1000
3
1 1 1
13
1 2 3 4 5 6 7 8 9 10 11 12 13
2
2 1
6
1 1 1 1 1 1
7
1 1 1 1 1 1 1

Output
6 23 21
1 1000 0
2 1 2
6 45 46
2 2 1
3 4 2
4 4 3

2.source code

#include<stdio.h>
#include<stdlib.h>

void game(const int * p,int save[],int r){
    int totA = 0,totB = 0,sigma = 0,tem = 0,mid;
    for(int i = 0;i < r-1;i++){
        sigma += p[i+1];
    }
    int k =0,n = 0,bool = 0,count = 0;
    while((sigma - totA - totB) > tem){
        if(count == 0){
            totA += p[k+1];
            k += 1;
            mid = totA;
            tem = mid;
            bool = 1;
            count += 1;
            continue;
        }
        if((sigma - totA - totB) > tem) count += 1;
        if(bool){
            tem = 0;
            while(tem <= mid){
                tem += p[r-n-1];
                n += 1;
            }
            totB += tem;
            mid = tem;
            bool = 0;
        }else{
            tem = 0;
            while(tem <= mid){
                tem += p[k+1];
                k += 1;
            }
            totA += tem;
            mid = tem;
            bool = 1;
        }
    }
    if(sigma - totA - totB) count += 1;
    if(bool){
        totB += sigma - totA - totB;
    }else{
        totA += sigma - totA - totB;
    }
    save[0] = count;
    save[1] = totA;
    save[2] = totB;
}


int main(){
    int t,n,save[3];
    scanf("%d",&t);
    int ** p = (int **)malloc(t*sizeof(int));
    for(int i = 0;i < t;i++){
        scanf(" %d",&n);
        p[i] = (int *)malloc((n+1)*sizeof(int));
        for(int k = 0;k < n;k++){
            scanf(" %d",&p[i][k+1]);
        }
        p[i][0] = n+1;
    }
    for(int i = 0;i < t;i++){
        save[0] = save[1] = save[2] = 0;
        game(p[i],save,p[i][0]);
        printf("%d %d %d\n",save[0],save[1],save[2]);
        free(p[i]);
    }
    free(p);
}

总结

因为不是软件程序,所以省略掉了判断空指针的过程。
同时,在编写过程中缺少了伪代码或者流程的搭建,导致对代码修改较为困难。
建议:
1.先写伪代码流程图。
2.多多练习

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值