ZeroJudge B294: Economic Crisis

On January 1st, 2505, there was a worldwide economic crisis. From that day on, prices soared. The price for one bun was one dollar on the first day, two dollars on the second day, three dollars on the third day, and so on.

Given the number of buns Wen-Wen bought each day starting from the first day, how much money did he spend in total?

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
The first line contains an integer N, representing the number of consecutive days Wen-Wen bought buns starting from the first day.
The second line will contain N integers, representing the number of buns Wen-Wen bought each day from the first day to the N-th day.
Output the total amount of money Wen-Wen spent on buns.
5
1 2 3 4 5
55

Thought Process

Use a for loop to iterate from 1 to N, multiplying the received data by i and adding it to a sum variable. Finally, output the value of the sum variable.

Sample Code-ZeroJudge B294: Economic Crisis

#include <iostream>
#include <stdio.h>
using namespace std;

int main() {
  int N;
  scanf("%d", &N); 
  int ans = 0;
  for (int i = 1; i<=N; i++)
    {
      int tmp;
      scanf("%d", &tmp);
      ans += tmp * i;
    }
  printf("%d\n", ans);
}

//Z.O.J. B294
//Dr. SeanXD

Comments