Advertisement
Graf_Rav

90 Zhukov

May 22nd, 2018
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.70 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main(){
  6.     int n;
  7.     cin>>n;
  8.    
  9.     int negativeCounter=0;
  10.     int first=1;
  11.     int last=1;
  12.    
  13.     int a;
  14.    
  15.     int ans=-0x80000000;
  16.     int ans2=-0x80000000;
  17.     int product=1;
  18.     int nymCounter=0;
  19.     for(int i=0;i<n;i++){
  20.         cin>>a;
  21.         if(a>0){
  22.             product*=a;
  23.             nymCounter++;
  24.             if(negativeCounter==0){
  25.                 first*=a;
  26.             }
  27.             else{
  28.                 last*=a;
  29.             }
  30.         }
  31.         else if(a<0){
  32.             nymCounter++;
  33.             product*=a;
  34.             if(negativeCounter==0){
  35.                 first*=a;
  36.             }
  37.             if(negativeCounter==1){
  38.                 ans=max(ans, last);
  39.             }
  40.             last=a;
  41.             negativeCounter++;
  42.         }
  43.         else{//Если встречаем 0
  44.             if(product<0){
  45.                 if(nymCounter!=1){
  46.                     ans2=product/first;
  47.                     ans2=max(ans2, product/last);
  48.                 }
  49.                 else{
  50.                     ans2=max(ans2, product);
  51.                 }
  52.                 ans2=max(ans2, 0);
  53.             }
  54.             else{
  55.                 ans2=product;
  56.             }
  57.             ans=max(ans, ans2);
  58.             negativeCounter=0;
  59.             first=1;
  60.             last=1;
  61.             product=1;
  62.             nymCounter=0;
  63.         }
  64.     }
  65.    
  66.     if(product<0){
  67.         if(nymCounter!=1){
  68.             ans2=product/first;
  69.             ans2=max(ans2, product/last);
  70.         }
  71.         else{
  72.             ans2=max(ans, product);
  73.         }
  74.         cout<<max(ans, ans2);
  75.     }
  76.     else{
  77.         cout<<max(product,  ans2);
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement