看過去小程式的疑惑

來源:互聯網
上載者:User

標籤:

問題 C 最大乘積
時間限制: 1 Sec 記憶體限制: 128 MB
[提交]
題目描述

輸入n個元素組成的序列S,你需要找出一個乘積最大的連續子序列,如果這個最大的乘積不是正數,則輸出-1

輸入

輸入包括多組資料,每組資料第一行為正整數n,第二行為n個元素組成的序列S,1<=n<=18,-10<=Si<=10

輸出

輸出每組資料的結果後換行

範例輸入
3
2 4 -3
5
2 5 -1 2 -1
範例輸出
8

20

本來不是很難得一道題 可是連續的應該不是必須從首位開始的吧?

接下來解釋為什麼會有這個問題

先貼oj通過的程式(copy別人的此處引用)

#include<iostream>#include<string.h>#include<stdio.h>#include<ctype.h>#include<algorithm>#include<stack>#include<queue>#include<math.h>using namespace std;int a[30];int i,j;int main(){    int n;    long long  w;    long long ans;    while(scanf("%d",&n)!=EOF)    {        for(i=0; i<n; i++)            scanf("%d",&a[i]);        ans=0;        for(i=0; i<n; i++)        {            w=1;            for(j=0; j<n; j++)            {                w*=a[j];                if(w>ans)                    ans=w;            }        }        if(ans>0)cout<<ans<<endl;        else            printf("-1\n"); }    return 0;}
不懂第二個for迴圈有什麼作用 既然接下來的for迴圈每次都是j=0開始 那麼執行n次並無意義

而且只能找到從首位開始乘積最大的子序列 不可以找到中間最大的子序列


接下來是改的第三個for迴圈

#include<iostream>#include<string.h>#include<stdio.h>#include<ctype.h>#include<algorithm>#include<stack>#include<queue>#include<math.h>using namespace std;int a[30];int i,j;int main(){    int n;    long long  w;    long long ans;    while(scanf("%d",&n)!=EOF)    {        for(i=0; i<n; i++)            scanf("%d",&a[i]);        ans=0;        for(i=0; i<n; i++)        {            w=1;            for(j=i; j<n; j++)            {                w*=a[j];                if(w>ans)                    ans=w;            }        }        if(ans>0)cout<<ans<<endl;        else            printf("-1\n");    }    return 0;}

改為j=i開始 保證了最大子序列不包括開頭

接下來帖自己寫的

#include<stdio.h>#define N 0int main(){    int a[20]={N},zero[20]={0};    int num0,num,num1,fu1,fu2,most,num10,most1,result;    int n,m,k;    while   (scanf("%d",&n)!=EOF){    num0=num=num1=m=fu1=fu2=num10=0;    most=most1=1;    k=1;    result=0;    while(k<=n){        scanf("%d",&a[k]);        if(a[k]==0){            zero[++m]=k;////0所在的位置            num0++;///0的個數        }        if(a[k]==10)            num10++;        k++;    }    if((n==1 && a[1]<1) || num0==n)        printf("-1\n");    else    {//else0        if(num10==n){            printf("1");            for(int b=0;b<n;b++)            printf("0");            printf("\n");}        else{    zero[++m]=n+1;    for(int i=0;i<num0+1;i++){//for0        for(int j=zero[i]+1;j<zero[i+1];j++){//for1            if(a[j]<0)                num++;///0與0之間的負數個數            else                num1++;        }//for1        if((num%2)==0 && (num1!=0 || num!=0)){//if1////當兩個0中間的數的負數為偶數且有非0數            for(int p=zero[i]+1;p<zero[i+1];p++){//for2                most=most*a[p];////求出當前積            }//for2        }//if1        else{//else1            if((num%2)!=0 && (zero[i+1]-zero[i])>2){            for(int q=zero[i]+1;q<zero[i+1];q++){///for3                if(a[q]<0)                    fu1++;                if(fu1<num)                    most=most*a[q];                if(a[zero[i+1]-q+zero[i]]<0)                    fu2++;                if(fu2<num)                    most1=most1*a[zero[i-1]-q+zero[i]];            }//for3            if(most<most1)                most=most1;            }            else                most=a[zero[i]+1];        } //else1        if(result<most )            result=most;        most=most1=1;        num=num1=0;    }//for0    if(result>0)    printf("%d\n",result);    else        printf("-1\n");    num10=0;        }    }//else0    }return 0;}

可是oj只能通過第一個

如果輸入

9

0 -1 0 2 3 0 7 8 0

oj通過的程式得到的是-1

改過的和自己的 得到的是56

是我沒讀懂題 還是為什麼

看過去小程式的疑惑

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.