Though programmers are quite clever, our friend Jake finds himself in prison for
ID: 3905865 • Letter: T
Question
Though programmers are quite clever, our friend Jake finds himself in prison for crimes he did not commit. He has to make his escape and after spending a lot of time looking at the bars in his cell window, he has realized that he can only remove a limited number of bars from his window before the guards hear and prevent his escape. If Jake has a square window with an equal number of vertical bars and horizontal bars, how big of a hole can he create by removing the bars from his window so that he can escape without the guards noticing? The input of the function is going to be the number of bars Jake can remove before the guards catch him You, as Jake's friend, should write a function that will return the largest hole that he can create if he removes all of the bars he can before the guards see YOUR ANSWER We recommend you take a quick tour of our editor before you proceed. The timer will pause up to 90 seconds for the tour Start tour Original codeC 1Winclude 0 Complete the function below. { 12 ? int accomplice( int numberOfBars) 13 14 15 16 17int main)) 37 Line: 10 Col: 1Explanation / Answer
the biggest hole can be created when we remove vertical and horizontal bars alternatively . Because its a square window, we keep variables H and V as 1 and keep increasing them one by one as long as we can still remove bars.
We then return their product (the area of the hole) as our answer.
int accomplice(int numberOfBars)
{
int h=1,v=1; int flag=1;
while(numberOfBars>0)
{
if(flag)
{
h++;
flag=0;
}
else
{
v++;
flag=1;
}
numberOfBars--;
}
return h*v;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.