思路
- 题意:就是一个人a从左边开始吃糖,一个人b从右边开始吃糖,每次吃的时候吃的糖必须比上一个人吃的多,求要几步才能吃完所有糖,并且输出每个人吃的糖的数量
- 就是一个简单模拟
代码
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
const int maxn=1e9+7;
int v[1010];
int main(){
int T;
cin >> T;
while(T--){
int a=0;int b=0;int n;
cin >> n;
for(int i=0;i<n;i++){
cin >> v[i];
}
int nowa=0;int nowb=0;
int posa=-1;int posb=n;
int flag=0;
int sum=0;
while(posa<posb){
if(!flag){
if(nowa>nowb){
nowb=0;
flag=1;
sum++;
}else{
if(posa+1>=posb){
if(nowa)sum++;
break;
}
posa++;
nowa+=v[posa];
a+=v[posa];
}
}
else{
if(nowa<nowb){
nowa=0;
flag=0;
sum++;
}else{
if(posb-1<=posa){
if(nowb)sum++;
break;
}
posb--;
nowb+=v[posb];
b+=v[posb];
}
}
}
cout << sum << " " << a << " " << b << "\n";
}
}