ZeroJudge E339: Prefix Practice

Definition: When B[i] is A[0]+A[1]+…+A[i], the array B is the prefix sum of array A.

定義 : 當 B[i] 為 A[0] + A[1] + … A[i],B 陣列為 A 陣列的前綴和。

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
The first line of input contains an integer N (1 ≤ N ≤ 200000), representing the size of array A. The second line contains N integers separated by spaces, representing A[0], A[1], A[2], ..., and A[N−1] respectively. The absolute value of numbers in the array will not exceed 10^9.Output a single line containing N integers separated by spaces, representing B[0], B[1], B[2], ..., and B[N−1] respectively.
5
1 2 3 4 5
1 3 6 10 15

Thought Process

You can use a for loop to collect the data and utilize a variable to store the current prefix sum. Please note that this problem requires the use of Long Long Integers.

Sample Code-ZeroJudge E339: Prefix Practice

#include <iostream>
using namespace std;

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  long long int N, sum = 0;
  cin >> N;
  for (int i = 0; i<N; ++i)
    {
      long long int tmp;
      cin >> tmp;
      sum += tmp;
      cout << sum << " ";
    }
  cout << "\n";
}

//ZeroJudge E339
//Dr. SeanXD

Comments