UVA 10059 - Maximum Product

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.

題目大意
給出一段長(zhǎng)度為 N(N≤ 18)的整數(shù)序列,請(qǐng)你找出其中乘積最大且為正值的連續(xù)子序列,輸出乘積值。

#include <stdio.h>
#include <string.h>
#define T 30   
//30為數(shù)組最大長(zhǎng)度

int main()
{   
    int d,M=0;//M為組號(hào)
    while(scanf("%d",&d)!=EOF&&d<=T){
    long long pmax=0;//最大乘積
    int aray[T];//輸入的數(shù)組
    memset(aray,0,sizeof(aray));

    for (int i = 0; i < d; i++)
    {
        scanf("%d",&aray[i]);

    }
    for (int i = 0; i < d; i++)
    {
        long long p=1;
        for (int j = i; j < d; j++)
        {
            p*=aray[j];
            if (p>pmax)
                pmax=p;
        }

        }

        M++;
    printf("Case #%d: The maximum product is %lld.\n\n",M,pmax);

        }

    return 0;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,099評(píng)論 0 23
  • 無(wú)意中發(fā)現(xiàn) 窗外夜很靜 今晚月很亮 拉一拉被子 原來(lái)這就是 冬天的影子
    丁曉點(diǎn)閱讀 152評(píng)論 0 3
  • 檢查了自己的時(shí)間顆粒度,以天為單位low爆了!所以今天的分享自己罰站3分鐘。以后珍惜時(shí)間,爭(zhēng)取以小時(shí)為單位吧。
    Vivian_2b6f閱讀 214評(píng)論 0 1
  • 作為一個(gè)女孩子,誰(shuí)沒(méi)有幾十個(gè)瓶瓶罐罐呢? 看看每年各個(gè)節(jié)假日里面商場(chǎng)、電商促銷(xiāo)的主力都是護(hù)膚(化妝)品。 不過(guò)這也...
    大大姐說(shuō)閱讀 2,848評(píng)論 0 51

友情鏈接更多精彩內(nèi)容