题目概述:
给定一个整数,输出他的补数。
思路:
这道题比较简单,不写了O_O
示例:
Input: 5 Output: 2 Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
代码:
class Solution { public: int findComplement(int num) { int count,i=0,sum=0; while(num/2!=0) { count=num%2; if(count==0) { sum+=pow(2.0,i); } i++; num=num/2; } return sum; } };