11059 - Maximum Product

All about problems in Volume CX. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Re: 11059 - Maximum Product

Postby robz84 » Thu Aug 06, 2009 11:00 pm

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.
robz84
New poster
 
Posts: 3
Joined: Thu Aug 06, 2009 8:28 pm
Location: Hungary

Re: 11059 - Maximum Product

Postby mf » Fri Aug 07, 2009 12:24 pm

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)
mf
Guru
 
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland

Re: 11059 - Maximum Product

Postby robz84 » Fri Aug 07, 2009 4:34 pm

aah, i got AC with the same code, but using Scanner.
thanks for the hint mf, it was very annoying for me.
robz84
New poster
 
Posts: 3
Joined: Thu Aug 06, 2009 8:28 pm
Location: Hungary

Re: 11059 - Maximum Product

Postby jhosimar3001 » Sun Mar 07, 2010 1:59 am

I've got AC. I corrected my mistake...
jhosimar3001
New poster
 
Posts: 3
Joined: Sun Mar 07, 2010 1:56 am

Re: 11059 - Maximum Product

Postby mathgirl » Tue May 08, 2012 2:03 pm

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;
}
mathgirl
New poster
 
Posts: 36
Joined: Tue Apr 24, 2012 6:20 pm

Re: 11059 - Maximum Product

Postby brianfry713 » Wed May 09, 2012 3:50 am

Doesn't match the sample I/O.
brianfry713
Guru
 
Posts: 1765
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11059 - Maximum Product

Postby mathgirl » Thu May 10, 2012 10:25 am

Thanks you ! I realised my mistake, I was resetting the sum to zero after a negative number. Got AC.
mathgirl
New poster
 
Posts: 36
Joined: Tue Apr 24, 2012 6:20 pm

Re: 11059 - Maximum Product

Postby 3sam » Fri Jun 29, 2012 8:05 pm

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[i];
}
long long int result=1;
for(int i=0;i<=x;i++)
{

result*=abs(arr[i]);
if(result>=max && result>=0)
{
max=result;
}
}
}
cout<<"Case #"<<j<<": The maximum product is "<<max<<"."<<endl;
cout<<endl;
j++;
}
return 0;
}
3sam
New poster
 
Posts: 6
Joined: Mon Apr 09, 2012 8:33 pm

Re: 11059 - Maximum Product

Postby brianfry713 » Sat Jun 30, 2012 12:19 am

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
brianfry713
Guru
 
Posts: 1765
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11059 - Maximum Product

Postby 3sam » Sat Jun 30, 2012 3:59 am

are you mean that if i use #include<stdlib.h> i can get AC ??????????????? :D :D :D
3sam
New poster
 
Posts: 6
Joined: Mon Apr 09, 2012 8:33 pm

Re: 11059 - Maximum Product

Postby brianfry713 » Mon Jul 02, 2012 10:13 pm

No, your code doesn't match the sample I/O. I was answering your question about why you got a Compile Error.
brianfry713
Guru
 
Posts: 1765
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11059 - Maximum Product

Postby gc_tushar » Thu Jul 12, 2012 8:50 pm

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);


    }

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

Re: 11059 - Maximum Product

Postby brianfry713 » Tue Jul 17, 2012 3:27 am

After each test case you must print a blank line.
brianfry713
Guru
 
Posts: 1765
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11059 - Maximum Product

Postby ibrahim_habib » Mon Jul 23, 2012 8:35 pm

this is my code , each time I submit this problem i got WA :x , 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;
}
ibrahim_habib
New poster
 
Posts: 1
Joined: Mon Jul 23, 2012 8:29 pm

Re: 11059 - Maximum Product

Postby brianfry713 » Tue Jul 24, 2012 1:31 am

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.
brianfry713
Guru
 
Posts: 1765
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

PreviousNext

Return to Volume CX

Who is online

Users browsing this forum: No registered users and 1 guest