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.多多练习