From f13dfb8c438c3c565ed4ede430e6046f54e5c9d5 Mon Sep 17 00:00:00 2001 From: Haidong Ji Date: Sat, 9 May 2020 15:32:19 -0500 Subject: Machine rebuilt so I had to reconfig Eclipse and googletest for C++ dev work on Linux --- Sources/GraphAlgo.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Sources/GraphAlgo.cpp (limited to 'Sources') diff --git a/Sources/GraphAlgo.cpp b/Sources/GraphAlgo.cpp new file mode 100644 index 0000000..d04987f --- /dev/null +++ b/Sources/GraphAlgo.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include + +using std::vector; + +static long getMaxDotProduct(vector a, vector b) { + std::sort(a.begin(), a.end()); + std::sort(b.begin(), b.end()); + long result = 0; + for (int i = 0; i < b.size(); i++) { + result = (long) a[i]*b[i]+result; + } + return result; +} + +TEST(MaxDotProduct, Max1) { + vector a = { 60, 100, 120 }; + vector b = { 20, 50, 30 }; + ASSERT_EQ(getMaxDotProduct(a, b), 10200); +} + +//TEST(MaxDotProduct, Max2) { +// vector a = { 23 }; +// vector b = { 39 }; +// ASSERT_EQ(getMaxDotProduct(a, b), 897); +//} +// +//TEST(MaxDotProduct, Max3) { +// vector a = { 1,3,-5 }; +// vector b = { -2,4,1 }; +// ASSERT_EQ(getMaxDotProduct(a, b), 23); +//} + +//int main() { +// size_t n; +// std::cin >> n; +// vector a(n), b(n); +// for (size_t i = 0; i < n; i++) { +// std::cin >> a[i]; +// } +// for (size_t i = 0; i < n; i++) { +// std::cin >> b[i]; +// } +// std::cout << getMaxDotProduct(a, b) << std::endl; +//} -- cgit v1.2.3