输入
4
1 1 1 3
输出
2
思路
回文两端相等,使用左右指针碰撞,相等前进,不等,从小的一边加
代码
#include<iostream>
using namespace std;
int main(){
int n;
int a[55];
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
}
int l=0;
int r=n-1;
int res=0;
while(l<r){
if(a[l]==a[r]) {
l++;
r--;
}
else if(a[l]<a[r]){
a[l+1]+=a[l];
l++;
res++;
}
else {
a[r-1]+=a[r];
r--;
res++;
}
}
cout<<res<<endl;
}