zoukankan      html  css  js  c++  java
  • Maximun product

     

    Given a sequence of integers S = {S1, S2, ..., Sn}, you shoulddetermine what is the value of the maximum positive product involving consecutive terms of S. Ifyou cannot find a positive sequence, you should consider 0 as the value of the maximum product.

     

    Each test case starts with 1 ≤ N ≤ 18, the number of elements in a sequence. Each elementSi is an integer such that -10 ≤ Si ≤ 10. Next line will haveN integers, representing the value of each element in the sequence. There is a blank line aftereach test case. The input is terminated by end of file (EOF).

     

    For each test case you must print the message: Case #M: The maximum product is P., where M isthe number of the test case, starting from 1, and P is the value of the maximum product. Aftereach test case you must print a blank line.

     

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


    枚举每一个长度组合,不断更新最大值,不过要注意有可能18个数都是10,那么最大值是多少?int还存得下吗?

    #include"iostream"
    #include"cstring"
    using namespace std;
    int main()
    {
     long long ans=-99999999;
     int ch,f;
     long long an=1;
     int a[20];
     f=1;
     int n;
     while(cin>>n)
     {
        for(int i=0;i<n;i++)
        {
            cin>>ch;
            a[i]=ch;
            an*=ch;
            if(an>ans)
            {
             ans=an;
            }
        }
        for(int j=n-1;j>=0;j--)
        {
            an=1;
            for(int k=j;k>=0;k--)
            {
            an*=a[k];
            if(an>ans)
            {
             ans=an;
            }
            }
        }
        if(ans<0) ans=0;
        cout<<"Case #"<<f++<<": The maximum product is "<<ans<<'.'<<endl;
        cout<<endl;
        ans=-99999999;
        an=1;
     }
    
    
        return 0;
    }
    View Code

     



  • 相关阅读:
    [转]maven插件的开发
    开发建议
    [转]利用maven的surefire插件实现单元测试与集成测试
    sonar-maven-plugin错误2
    20145218张晓涵_网络欺诈技术防范
    20145218张晓涵_信息搜集与漏洞扫描
    20145218张晓涵_Exp5 MSF基础应用
    20145218张晓涵 PC平台逆向破解_advanced
    20145218张晓涵 恶意代码分析
    20145218 免杀原理与实践
  • 原文地址:https://www.cnblogs.com/zsyacm666666/p/4680111.html
Copyright © 2011-2022 走看看