zoukankan      html  css  js  c++  java
  • HUAS Summer Trainning #3 B

    题目:

    Given a sequence of integers S = {S1, S2, . . . , Sn}, you should determine what is the value of the
    maximum positive product involving consecutive terms of S. If you cannot find a positive sequence,
    you should consider 0 as the value of the maximum product.
    Input
    Each test case starts with 1 ≤ N ≤ 18, the number of elements in a sequence. Each element Si
    is
    an integer such that −10 ≤ Si ≤ 10. Next line will have N integers, representing the value of each
    element in the sequence. There is a blank line after each test case. The input is terminated by end of
    file (EOF).
    Output
    For each test case you must print the message: ‘Case #M: The maximum product is P.’, where
    M is the number of the test case, starting from 1, and P is the value of the maximum product. After
    each test case you must print a blank line.
    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.

    题目大意:输入个长度为n的序列,要你找到乘积最大的连续序列的积。(如果乘积小于0,就相当于乘积为0)

    题目思路:既然要找连续的序列,给一个循环枚举起点,一个循环枚举终点,一个循环它起点到终点的元素乘起来,给个MAX变量赋值为0(因为乘积小于0点都会被赋值为0)。

    X每个乘积都与MAX比较,比MAX大的,就把它赋值给MAX。循环完毕,输出MAX的值就行了。(还要注意输出案例时要连着输出2个换行)

    代码:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 using namespace std;
     5 const int maxn=18+5;
     6 int main()
     7 {
     8     int n,i,a[maxn],j,k,p=0;
     9     long int  sum,max; 
    10     while(cin>>n&&n)
    11     {        
    12         for(i=0;i<n;i++)
    13             cin>>a[i];
    14         max=0;
    15         for(i=0;i<n;i++)
    16         {
    17             for(j=i;j<n;j++)
    18             {
    19                 sum=1;
    20                 for(k=i;k<=j;k++)
    21                 {
    22                     sum*=a[k];
    23                 }
    24                 if(sum<0)
    25                     sum=0;
    26                 if(sum>max)
    27                     max=sum;
    28             }
    29         }
    30         printf("Case #%d: The maximum product is %lld.
    
    ",++p,max);
    31         
    32     }
    33     return 0;
    34 }
  • 相关阅读:
    Redis (error) NOAUTH Authentication required.问题
    C# File() 方法 contentType参数取值
    Java常用快捷键汇总(杂乱无章,持续更新)
    VUE项目发布至IIS(不涉及代理)
    js的休眠实现---sleep()
    Oracle 分页
    winform Panel设定 按钮显示位置
    Chart控件绘制实时折线图表——Spline
    .NET打包合并dll方法
    ComboBox绑定键值对
  • 原文地址:https://www.cnblogs.com/huaxiangdehenji/p/4694124.html
Copyright © 2011-2022 走看看