Problem : Ciel and Receipt
Problem Code: CIELRCPT
Is there a better to write the program?
How do I convert the huge list of statements to find the number of menus to a compact form using loops or recursion?
Here is my solution:
https://www.codechef.com/viewsolution/13884977
Since you know maximum value can be 2048 (211). You can simply iterate from 11 to 0, to find the optimal answer of counter required. Here, I’m mentioning possible optimal solution:
The logic followed is the iterate until you find a value smaller than input P (which would be a power of 2) and check it’s maximum frequency possible.
Example: P = 4096
Q = 2048 and Q < P so P -= Q and count += 1 check again in next while iteration: P = 2048 i.e. Q ==P after this iteration P = 0 and hence we break the loop. Hope the code helps.
int count=0; for(int i=11;i>=0;i--){ int q=(int)Math.pow(2, i); while(p>=q){ p=p-q; count++; } if(p==0) break; }
1 Like
Thanks.It helped me a lot!
1 Like