5 条题解

  • 1
    @ 2025-12-21 18:36:31
    #include <bits/stdc++.h>
    using namespace std;
    int main(){
        int n;
        cin>>n;
        int a[n+1];
        int j;
        int c=0;
        int b=0;
        for(int i=1;i<=n;i++){
            cin>>j;
            a[i]=j;
            b+=j;
        }
        for(int i=1;i<=n;i++){
            if(2*c+a[i]==b){
                cout<<i;
                return 0;
            }
            c+=a[i];
        }
        cout<<"-1";
    }
    
    • @ 2025-12-23 16:15:58

      很巧妙的写法,只用一个变量c来滚动计算(来一个新数就算一个)而没有提前开presum数组,空间复杂度低(没有提前开数组)。

      改进点1:这里前缀和是累计值,可能会超过int,最好写long long c

      改进点2: 变量命名尽量用英语单词缩写,方便理解意思

信息

ID
19320
时间
1000ms
内存
32MiB
难度
10
标签
(无)
递交数
6
已通过
4
上传者