Advertisement
maxpayne

Octal Code

Aug 21st, 2011
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. A non-empty array A of N elements contains octal representation of a non-negative integer K, i.e. each element of A belongs to the interval [0; 7]
  2.  
  3. Write a function:
  4.  
  5. int bitcount_in_big_octal(const vector<int> &A);
  6.  
  7. that returns the number of bits set to 1 in the binary representation of K. The function should return -1 if the number of bits set to 1 exceeds 10,000,000.
  8.  
  9. Assume that the array can be very large.
  10.  
  11. Assume that:
  12.  
  13. N is an integer within the range [1..100,000];
  14.  
  15.  
  16. My answer:
  17.  
  18. //this is java
  19. public int bitcount_in_big_octal ( int[] A ) {
  20. int count[] ={0,1,1,2,1,2,2,3};
  21. int sum =0;
  22. for(int i=0;i<A.length;i++){
  23. sum+=count[A[i]];
  24. }
  25. return sum;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement