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 };
原文地址:https://www.cnblogs.com/Asurudo/p/10630804.html
时间: 2024-10-17 01:52:33