UVA 11059 Maximum Product

xiaoxiao2021-02-27  149

简单的题目,直接暴力遍历计算即可,时间复杂度O(n*n),具体实现见如下代码:

#include<iostream> #include<vector> #include<string> #include<set> #include<stack> #include<queue> #include<map> #include<algorithm> #include<cmath> #include<iomanip> #include<cstring> #include<sstream> #include<cstdio> #include<deque> using namespace std; int n; int main(){ int Case = 0; while (cin >> n){ Case++; vector<int> data; long long int temp; long long int res = 0; for (int i = 0; i < n; i++){ int t; cin >> t; data.push_back(t); } for (int i = 0; i < n; i++){ temp =1; for (int j = i ; j < n; j++){ temp *= data[j]; if (temp > res) res = temp; } } cout << "Case #" << Case << ": The maximum product is " << res<<"." << endl << endl; } return 0; }

转载请注明原文地址: https://www.6miu.com/read-17270.html

最新回复(0)