-
Notifications
You must be signed in to change notification settings - Fork 1
/
2021 S1.cpp
57 lines (34 loc) · 1.17 KB
/
2021 S1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
Difficulty: Easy
Ad Hoc Problem
There's no fancy algorithm for this problem. You quite literaly just input the information given into the formula for the area of a trapezoid and sum up the areas.
Things that would be important to know for solving this question:
Formula for area of a trapezoid
For loops, while loops could be used as well.
Only thing to watch out for if you're using C++ is when you cout very large numbers, they may be in scientific notation, to prevent this, use std::fixed
*/
#include <iostream>
#include <vector>
int main(){
int N;
std::cin >> N;
//Collect heights of fences
std::vector<double> heights (N + 1);
for (int i = 0; i <= N; i++){
std::cin >> heights[i];
}
//Collect bases/widths of fences
std::vector<double> widths (N);
for (int i = 0; i < N; i++){
std::cin >> widths[i];
}
double areaSum = 0, trapezoidArea;
//Calculate area of the fences
for (int i = 0; i < N; i++){
trapezoidArea = (heights[i] + heights[i + 1]) / 2 * widths[i];
areaSum += trapezoidArea;
}
//To prevent scientific notation
std::cout << std::fixed << areaSum;
return 0;
}