## 11059 - Maximum Product

Moderator: Board moderators

robz84
New poster
Posts: 3
Joined: Thu Aug 06, 2009 8:28 pm
Location: Hungary

### Re: 11059 - Maximum Product

hi mf!

if you uncomment my previous code, there will be blank line.
i forgot to uncomment, sorry, but thanks for reply.

exactly i ve tried 3 possibilities, lets the last output is: Case #5: The maximum product is 54.

1.
Case #5: The maximum product is 54. <-- end of output

2.
Case #5: The maximum product is 54.
<-- end of output

3.
Case #5: The maximum product is 54.
<ENTER>
<-- end of output

but all of them is WA, so maybe something is wrong with the calculation, but i dont find the error, the program generates good answer all the sample from this topic and all the sample what i created

(sorry for my english.)

here is the original code (WA) :

Code: Select all

removed after AC
Last edited by robz84 on Fri Aug 07, 2009 4:36 pm, edited 1 time in total.

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

### Re: 11059 - Maximum Product

I don't see what's wrong, your code looks fine to me.

Maybe you should try to use java.util.Scanner and its nextInt() method to parse the input (it's more tolerable to whitespace in input, similar to C's scanf)

robz84
New poster
Posts: 3
Joined: Thu Aug 06, 2009 8:28 pm
Location: Hungary

### Re: 11059 - Maximum Product

aah, i got AC with the same code, but using Scanner.
thanks for the hint mf, it was very annoying for me.

jhosimar3001
New poster
Posts: 3
Joined: Sun Mar 07, 2010 1:56 am

### Re: 11059 - Maximum Product

I've got AC. I corrected my mistake...

mathgirl
New poster
Posts: 36
Joined: Tue Apr 24, 2012 6:20 pm

### Re: 11059 - Maximum Product

I ve got WA on this nearly 10 times ! I tried previous test inputs and they work fine. Can someone tell me what's wrong with this code ?

Code: Select all

#include<iostream>
#include<string>

using namespace std;

int main()
{
int n,count = 1;
string empty;

while(cin>>n)
{
long long ans = 1;
long long cur;
long long res = 0;

for(int i=0;i<n;i++)
{
cin >> cur;
if(cur > 0)
{
ans = ans*cur;
res = max(res,ans);
}
else
ans = 1;
}

cout << "Case #" << count << ": The maximum product is " << res << ".\n\n";
count++;
getline(cin,empty);
}
return 0;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 11059 - Maximum Product

Doesn't match the sample I/O.
Check input and AC output for thousands of problems on uDebug!

mathgirl
New poster
Posts: 36
Joined: Tue Apr 24, 2012 6:20 pm

### Re: 11059 - Maximum Product

Thanks you ! I realised my mistake, I was resetting the sum to zero after a negative number. Got AC.

3sam
New poster
Posts: 6
Joined: Mon Apr 09, 2012 8:33 pm

### Re: 11059 - Maximum Product

tgis is my code and i got every subm compile error not wrong answer ::
plz help

#include<iostream>
using namespace std;
int main()
{
long long int x,y;
int arr[100];
int z=1;
int j=1;
while(cin>>x)
{
int max=-10000000;
if(x==1)
{
cin>>y;
if(y>0)
{
cout<<"Case #"<<j<<": The maximum product is "<<y<<"."<<endl;
cout<<endl;
}
else
{
cout<<"Case #"<<j<<": The maximum product is "<<"0"<<"."<<endl;
cout<<endl;
}
}
else
{
for(int i=0;i<x;i++)
{
cin>>arr;
}
long long int result=1;
for(int i=0;i<=x;i++)
{

result*=abs(arr);
if(result>=max && result>=0)
{
max=result;
}
}
}
cout<<"Case #"<<j<<": The maximum product is "<<max<<"."<<endl;
cout<<endl;
j++;
}
return 0;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 11059 - Maximum Product

prog.cpp: In function ‘int main()’:
prog.cpp:36: error: ‘abs’ was not declared in this scope
prog.cpp:7: warning: unused variable ‘z’

#include <stdlib.h>

You can see the reason for your compile errors if you click My Submissions on the left or go to:
http://uva.onlinejudge.org/index.php?op ... e&Itemid=9
Check input and AC output for thousands of problems on uDebug!

3sam
New poster
Posts: 6
Joined: Mon Apr 09, 2012 8:33 pm

### Re: 11059 - Maximum Product

are you mean that if i use #include<stdlib.h> i can get AC ???????????????

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 11059 - Maximum Product

Check input and AC output for thousands of problems on uDebug!

gc_tushar
New poster
Posts: 1
Joined: Thu Jul 12, 2012 8:46 pm

### Re: 11059 - Maximum Product

Why I getting WA

Code: Select all

#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
#include<sstream>
#include <deque>
#define pb(a) push_back(a)
#define mx 1010
#define inf (1<<30)
#define pie 3.14159265358979323846
#define mems(arr,temp) memset(arr,temp,sizeof arr)

using namespace std;
//int dx[]= {0,0,-1,1};
//int dy[]= {1,-1,0,0};
//int dx[]= {-1,1,-2,2,-2,2,-1,1};
//int dy[]= {-2,-2,-1,-1,1,1,2,2};
main()
{
int n,t=0;

while(cin>>n)
{

long long int arr[100];

for(long long int i=0; i<n; i++)
{
cin>>arr[i];
}

long long int ans=0;
long long int temp=1;
for(int i=0; i<n; i++)
{
temp*=arr[i];
if(temp>ans) ans=temp;
}
temp=1;
for(int i=n-1; i>=0; i--)
{
temp*=arr[i];
if(temp>ans) ans=temp;
}

if(t>0) cout<<endl;
printf("Case #%d: The maximum product is %lld.\n",++t,ans);

}

}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 11059 - Maximum Product

After each test case you must print a blank line.
Check input and AC output for thousands of problems on uDebug!

ibrahim_habib
New poster
Posts: 1
Joined: Mon Jul 23, 2012 8:29 pm

### Re: 11059 - Maximum Product

this is my code , each time I submit this problem i got WA , please anyone helps me

Code: Select all

#include<iostream>
using namespace std;
int main()
{
int n, product[18] = {0}, counter = 1;
long long res;
while(cin >> n)
{
res = 1;
for(int i = 0; i < n; i ++)
cin >> product[i];

for(int i = 0; i < n - 1; i ++)
for(int j = i + 1; j < n; j ++)
if(product[i] < product[j])
swap(product[i], product[j]);
if ((n == 1 && product[0] <= 0) || (n == 2 && product[0] == 0))
res = 0;
else
for(int i = 0; i < n; i ++)
if(product[i] > 0)
res *= product[i];
else if(product[i] == 0)
continue;
else if(product[i] < 0)
if((n - i) % 2 == 0)
for( ; i < n; i ++)
res *= product[i];
else
for(i = i + 1; i < n; i ++)
res *= product[i];

cout <<"Case #" <<counter << ": The maximum product is " << res << "." << endl << endl;

counter ++;
}

return 0;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 11059 - Maximum Product

Input:

Code: Select all

3
2 0 2
AC output:

Code: Select all

Case #1: The maximum product is 2.

Last edited by brianfry713 on Mon Dec 10, 2012 10:08 pm, edited 1 time in total.
Check input and AC output for thousands of problems on uDebug!