• 3thweek2。uva11059 maximum product.


    题意简概:

    输入n个元素组成的序列S,你需要找一个乘积最大的连续子序列。如果这个最大的乘积不是正数,应输出0,表示无解。1<=n<=18,-10<=Si<=10。

    Sample Input
    3
    2 4 -3
    5
    2 5 -1 2 -1
    Sample Output
    Case #1: The maximum product is 8.
    Case #2: The maximum product is 20.

    简单分析:

    连续子序列有两个要素:起点和终点。因此只要枚举起点和终点即可。由于每个元素的绝对值不超过10且不超18个元素,最大可能的乘积不会超过10^18,用long long 型存储。

    代码如下:

     1 #include<iostream>
     2 #include<cstdio>
     3 using namespace std;
     4 int a[18];
     5 
     6 int main()
     7 {
     8     int n,x=0;
     9     while(scanf("%d",&n)!=EOF  && n>=1 && n<=18)
    10     {
    11         int i,j;
    12         int maxn=0;
    13         for(i=0;i<n;i++)
    14             scanf("%d",&a[i]);
    15         
    16         for(i=0;i<n;i++)
    17         {    
    18         int    k=1;            //注意k的位置。每次子序列起点换了时都要重新赋值为1
    19             for(j=i;j<n;j++)
    20             {   
    21                 k*=a[j];
    22               if(k>maxn)
    23                   maxn=k;      //maxn一直保存当前最大值。
    24             }
    25         }
    26         x++;
    27       printf("Case #%d: The maximum product is %d.
    
    ",x,maxn);
    28 
    29     }
    30      
    31       return 0;
    32 
    33 }

    尤其注意输出答案,例如Case的首字母大写,单词间的空格。

  • 相关阅读:
    IE9的css hack
    ie6 插入图片img png24 阴影
    clear:both; overflow:hidden
    ie6 背景图片 png24 阴影
    html 页面定位
    IE无法显示PNG
    行内元素 & 块元素
    div+css布局时的浏览器兼容问题
    删除源代码的管理信息(VSS)
    string path;
  • 原文地址:https://www.cnblogs.com/x512149882/p/4695811.html
Copyright © 2020-2023  润新知