Thursday, December 19, 2013

codechef EASYPROB - "easy problem" solution

codechef EASYPROB - "easy problem": http://www.codechef.com/problems/EASYPROB/

my c++ solution to codechef EASYPROB - "easy problem": http://ideone.com/7upexe
#include<stdio.h>
int main()
{
printf("137=2(2(2)+2+2(0))+2(2+2(0))+2(0)\n");
printf("1315=2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)\n");
printf("73=2(2(2)+2)+2(2+2(0))+2(0)\n");
printf("136=2(2(2)+2+2(0))+2(2+2(0))\n");
printf("255=2(2(2)+2+2(0))+2(2(2)+2)+2(2(2)+2(0))+2(2(2))+2(2+2(0))+2(2)+2+2(0)\n");
printf("1384=2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2)+2(2(2)+2(0))+2(2+2(0))\n");
printf("16385=2(2(2+2(0))+2(2)+2)+2(0)\n");
return 0;
}

my draft to get log2 of the numbers:
http://ideone.com/3AcHkc
#include <iostream>
#include <math.h>
using namespace std;

int main() {
// your code goes here
int a, b, arr[17], temp[17];
cin>>a; b=a;
for(int i=0; i<17; i++) {
arr[i]=pow(2, i);
}
for(int j=16; b>0; j--) {
while(b>=arr[j]) {
temp[j]=log2(arr[j]); b=b-arr[j];
cout<<temp[j]<<" "<<arr[j]<<" "<<b<<endl;
}
}
return 0;
}

No comments:

Post a Comment