Leetcode-1028 Convert to Base -2(负二进制转换)

 1 class Solution
 2 {
 3     public:
 4         string baseNeg2(int N)
 5         {
 6             string ss;
 7             int  n = N;
 8             stack<int> s;
 9             int a;
10             if(n == 0)
11                 return "0";
12             else
13             {
14                 while(n!=0)
15                 {
16                     a = abs(n%(-2));
17                     s.push(a);
18                     n = (n-a) / (-2);
19                 }
20             }
21             while(!s.empty())
22             {
23                 ss+=s.top()+'0';
24                 s.pop();
25             }
26             return ss;
27         }
28 };

猜你喜欢

转载自www.cnblogs.com/Asurudo/p/10630804.html
今日推荐