今天有做作業,根據參考程試,處理overflow之後,出現 Failed case #4/17: time limit exceeded (Time used: 1.99/1.00, memory used: 22884352/2147483648.),今天只做到這裡,希望明天能夠解決。
#include <iostream>
#include <vector>
#include <algorithm>
long long MaxPairwiseProduct(const std::vector<int>& numbers) {
long long max_product = 0;
int n = numbers.size();
for (int first = 0; first < n; ++first) {
for (int second = first + 1; second < n; ++second) {
max_product = std::max( max_product,
( long long ) numbers[first] * numbers[second]);
}
}
return max_product;
}
int main() {
int n;
std::cin >> n;
std::vector<int> numbers(n);
for (int i = 0; i < n; ++i) {
std::cin >> numbers[i];
}
std::cout << MaxPairwiseProduct(numbers) << "\n";
return 0;
}