Wednesday, 22 July 2015

EGYPIZZA

Standard


Pizza

Link to the question : EGYPIZZA 

HINT :

Easy question. Just stick to the basics and code all the way.

RECOMMENDED QUESTION :

Try your hands on this question .

SOURCE CODE :



#include<iostream>

#include<cmath>

#include<cstdio>

using namespace std;

int main()

{

int n,i=0,ans=0;

int a1=0,a2=0,a3=0;

int n1,n2;

char op;

cin>>n;

for(i=0;i<n;i++)

{

cin >> n1 >> op >> n2;

if(n1==3) a3++;

else if(n2==2) a2++;

else if(n2==4) a1++;

}

ans=a3;

a1=a1-a3;

ans+=(a2-a2%2)/2;

if(a2%2)

{

ans++;

a1-=2;

}

if(a1 > 0) {

ans+=(a1-(a1%4))/4;

if(a1%4>0) {

ans++;

}

}

cout<<ans+1<<endl;

return 0;

}



    


3 comments:

  1. your code shows ans 2 for test case..
    4
    1/4
    1/4
    1/4
    1/4

    while correct ans is 3(as per comments in question on spoj and http://spojtoolkit.com/test/EGYPIZZA)

    ReplyDelete
    Replies
    1. If you go with the question, 2 is the correct answer. You can ask those who commented 3 as the answer that why the answer will be 3 :p

      Delete
  2. What was wrong with this code, I know there must be something..
    Please explain
    #include
    using namespace std;
    int main()
    {
    int t,l=0,m=0,x,y;
    float j=0,k=0;
    char c;
    cin>>t;
    while(t--)
    { cin>>x>>c>>y;
    if(y==4)
    j+=(float)x;
    if(y==2)
    k+=(float)x;

    } j/=4;
    k/=2;
    l=(int)j;
    m=(int)k;
    if(j-l)
    l+=1;
    if(k-m)
    m+=1;
    printf("%d\n",l+m+1);
    return 0;
    }

    ReplyDelete