因为范围只有18个数字,所以可以枚举起点和终点。
1 #include <iostream> 2 #include <cstdlib> 3 #include <cstdio> 4 #include <cmath> 5 6 using namespace std; 7 8 long long int f(int a[], int start, int end) 9 { 10 long long int sum = 1; 11 for (int i = start; i < end + 1; ++i) 12 { 13 sum *= a[i]; 14 } 15 return sum; 16 } 17 18 int main(void) 19 { 20 int n, a[20]; 21 #ifndef ONLINE_JUDGE 22 freopen("in", "r", stdin); 23 #endif 24 while (~scanf("%d", &n)) 25 { 26 for (int i = 0; i < n; ++i) 27 scanf("%d", &a[i]); 28 long long int sum=-1; 29 for (int i = 0; i < n; ++i) 30 { 31 for (int j = i; j < n; ++j) 32 { 33 if (f(a, i, j) > sum) 34 sum = f(a, i, j); 35 } 36 } 37 printf("%lld\n", sum); 38 } 39 40 return 0; 41 }
根据数据范围,估算一下能不能暴力