fork download
  1. #include<stdio.h>
  2.  
  3. /**
  4.  * My custom recursive function to calculate power of two
  5.  */
  6. unsigned int twoPow(unsigned int n){
  7. if(n == 0)
  8. return 1;
  9.  
  10. /**
  11.   * this is our recursive counter we need to decrease it otherwise it will go to infinite loop
  12.   */
  13. n--;
  14.  
  15. /**
  16.   * simply keep multiplying 2 given times to get power of two
  17.   */
  18. return 2 * twoPow(n);
  19. }
  20.  
  21. int main(){
  22. char s[32];
  23. unsigned int i,j,sum; /* here i used unsigned since none of our input or outputs are negative */
  24.  
  25. while (gets(s)){
  26. /*
  27.   * check if its 0 then exit, Maybe even deleting the NULL check will work for this problem
  28.   */
  29. if(s[0] == '0' && s[1] == '\0')
  30. return 0;
  31.  
  32. sum = 0;
  33.  
  34. /**
  35.   * get the string length
  36.   */
  37. for(i = 0; s[i]; ++i);
  38.  
  39. for(j = 0; s[j]; ++j)
  40. /**
  41.   * convert character to integer, then multiply 2 to the power n, then subtract 1 and add to sum
  42.   */
  43. sum = sum + (s[j] - 48) * (twoPow(i - j) - 1);
  44.  
  45. printf("%u\n", sum);
  46. }
  47. return 0;
  48. }
Success #stdin #stdout 0s 2116KB
stdin
10120
200000000000000000000000000000
10
1000000000000000000000000000000
11
100
11111000001110000101101102000
0
stdout
44
2147483646
3
2147483647
4
7
1041110737