This is a codechef beginner problem. I know it can be done in much easier ways, but I thought to try recursion here. My program is giving correct answer when I give only one input at a time, but when I run given testcases it only shows output for first two.
https://www.codechef.com/problems/CIELRCPT
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
long t,p,premaining;
int counts=0;
int menus(int premaining)
{
long k=0;
long j=0;
if(premaining>=2048)
{
counts=premaining/2048;
premaining=premaining%2048;
if(premaining==0)
{
return counts;
}
}
while(k<=premaining)
{
j++;
k=pow(2,j);
}
if(j==1){k=1;}
else
{k=pow(2,j-1);}
premaining-=k;counts++;
//cout<<"j-1 is "<<(j-1)<<" k is "<<k<<" premaining for "<<counts<<" is"<<premaining<<endl;
k=0,j=0;
if(premaining!=0) {menus(premaining);}
else{ return counts; }
}
int main() {
cin>>t;
while(t--)
{
cin>>p;
counts=0;
premaining=p;
for(long i=0;i<11;i++)
{
long k=pow(2,i);
if(k==p)
{
cout<<"1"<<endl;
return 0 ;
}
}
long menucount =menus(premaining);
cout<<menucount<<endl;
}
return 0;
}