#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
vector<int> A = {2, 4, 6, 8, 10}; // Ví dụ vector
int n = A.size();
// Tạo mảng tổng cộng dồn S
vector<int> S(n);
S[0] = A[0];
for (int i = 1; i < n; ++i) {
S[i] = S[i-1] + A[i];
}
// Ví dụ tính tổng subarray từ i đến j
int i = 0, j = 3; // Thay đổi i và j để kiểm tra
int sum;
if (i == 0) {
sum = S[j];
} else {
sum = S[j] - S[i-1];
}
cout << "Tổng của subarray từ " << i << " đến " << j << " là: " << sum << endl;
return 0;
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter