思路:
最大值,一个一个乘,然后进行比较,要是最大值还是为0的话就是做是没有的。
N最大可以为18,所以max比较大,用long long。
#include<iostream> #include<stdio.h> using namespace std; #define M 18 int n=0; int main() { int N; while(scanf("%d",&N)!=EOF) { int a[M]={0}; for(int i=0;i<N;i++) scanf("%d",&a[i]); long long int max=0; for(int i=0;i<N;i++) { long long int mul=1; for(int j=i;j<N;j++) { mul*=a[j]; if(mul>max) max=mul; } }//确定最大值 n++; if(max>0) printf("Case #%d: The maximum product is %lld.\n",n,max); else printf("Case #%d: The maximum product is 0.\n",n); cout<<endl; } return 0; }