Monday 21 January 2013

TopCoder SRM 567 250 Problem


The Ninja Turtles often battle the Foot Clan ninjas. The Turtles celebrate each victory with a pizza party. The amount of pizza they eat depends on the number of opponents they have defeated. Denote the number of defeated opponents as N. Three of the four Turtles have a moderate appetite and only consume floor(N / K) pizzas each. The fourth Turtle is always hungry and eats floor(N / 3) pizzas.

You are given ints P and K, where P is the total number of pizzas the Turtles ate after a battle. If there exists at least one value of N such that after defeating N opponents the Turtles would eat exactly P pizzas at the party, return the smallest such N. Otherwise, return -1.

I have solved this in a simple way...check it out...:)

#include<iostream>
#include<cmath>
using namespace std;
class NinjaTurtles
{
public:
int countOpponents(int P , int K )
{
int x = 1;
int temp = 0 , temp1 = 0;
int y = (int)((3*K*P) / ( K + 3) );
cout<<y<<endl;
while( (x < K) || (x < 3))
{
            temp = (int)floor(x/K);
temp1 = (int)floor(x/3);
            temp = temp * 3;
            if((temp + temp1) == P)
{
return x;
}  
x++;
            }
while(x <= y)
{
            temp = (int)floor(x/K);
temp1 = (int)floor(x/3);

temp = temp * 3;
if((temp + temp1) == P)
{
return x;
}
x++;
}
return -1;
}
};
                                                                               BY:  SAURABH BHATIA



No comments:

Post a Comment