前缀和 s[i] 表示前 i 个数的和,区间 [l,r] 的和为 s[r]-s[l-1]。输入 n 和 n 个整数,输出全部前缀和。
#include <bits/stdc++.h>
using namespace std;
int main() {
int n; cin >> n;
vector<long long> s(n+1);
for (int i=1; i<=n; ++i) { long long x; cin >> x; s[i]=s[i-1]+x; }
for (int i=1; i<=n; ++i) cout << s[i] << (i==n?'\n':' ');
}