codechef CIELRCPT - "ciel and receipt" problem: http://www.codechef.com/problems/CIELRCPT
my c++ solution to codechef CIELRCPT - "ciel and receipt" problem: http://ideone.com/9pmILI
#include <iostream>
#include <cmath>
using namespace std;
int main() {
// your code goes here
int t, p, arr[12], count;
for(int i=0; i<12; i++) arr[i]=pow(2, i);
cin>>t;
while(t--) {
count=0;
cin>>p;
while(p>0) {
for(int i=11; i>=0; i--) {
if(p>=arr[i]) {
p=p-arr[i]; count++; i++;
}
}
}
cout<<count<<endl;
}
return 0;
}
note on my code: initialize the array of power 2, starting from 0 goes to 11. after getting p, starting from the maximum value of array(here 2048, index is arr[11]=2048) and check if p is equal to or greater than that value. if so, p=p-arr[i], count++. i also increased i++ so that continues from the index last visited. thnx
why did you use p=p-arr[i]
ReplyDelete