Saturday, 11 August 2012
PIZZA
SPOJ Problem Set (classical)
7169. Pizza
Problem code: EGYPIZZA
Abotrika is having a party because his team won the african cup so he is inviting his friends to eat some pizza.
Unfortunately,Abotrika's friends can't eat an entire pizza but all of them know exactly how much pizza
they can eat and insist on getting the exact amount of pizza but Abotrika eats one complete pizza
and all of them wants his amount of pizza in one slice.
Their requests break down to three different pizza slices-either one quarter or a half or three quarters of pizza.
write a program that will help Abotrika to find out what is the minimal number of pizzas he has to order so that
everyone gets exact amount of pizza they want.
Input
First line contains an integer N, 0<=N<=10,000 , number of friends.
In each of next N lines there is amount of pizza that each of Abotrika's friends wants to eat,that
is the fraction 1/4 , 1/2 or 3/4.
Output
In the first and only line you should write the minimal number of pizzas Abotrika has order don't forget to order
one complete pizza for Abotrika
Example
Input:
3
1/2
3/4
3/4
Output:
4
Input:
5
1/2
3/4
1/2
1/4
1/4
Output:
4
#include
int main()
{
int t,a,b,i,half=0,quarter=0,third_quarter=0,pizza=0;
char c;
scanf("%d",&t);
for(i=0;i
{
scanf("%d%c%d",&a,&c,&b);
if(a==1&&b==2)
half=half+1;
else if(a==1&&b==4)
quarter=quarter+1;
else
third_quarter=third_quarter+1;
}
if(quarter==0)
{
pizza=third_quarter+(half/2)+(half%2);
}
else{
if(quarter<=third_quarter)
pizza=third_quarter+(half/2)+(half%2);
if(quarter>third_quarter)
{
pizza+=third_quarter;
quarter=quarter-third_quarter;
if(quarter>2*half)
{
pizza+=half+(2*half-quarter);
}
else if(quarter==2*half)
{
pizza+=half;
}
else if(quarter<2 font="font" half="half">2>
{
int half1;
half1=(quarter/2)+(quarter%2);
half=half-half1;
pizza+=half1+(half/2)+(half%2);
}
}
}
printf("%d\n",pizza+1);
return 0;
}
1 comment:
it's a nice code but there are some mistakes in b/w like for loop and it should be quater-2*half in the else if block
Post a Comment