代码如下:
#include <iostream>
using namespace std;
const int N = 1010;
int a[N], s[N];int main() {int n;cin >> n;for (int i = 1; i <= n; i++) {cin >> a[i];//原数组s[i] = s[i - 1] + a[i];}for (int i = 1; i <= n; i++)cout << s[i] << " ";//输出前缀和数组cout << endl;int l, r;cin >> l; //左边cin >> r; //右边cout << s[r] - s[l - 1] << endl;//输出区间[l,r]的所有元素总和return 0;}
测试结果: