天天看點

poj3191(進制轉換)

題目連結:http://poj.org/problem?id=3191

題意:将一個數轉換為-2為基數的數

思路:套路,看代碼就好了

代碼:

1 #include <iostream>
 2 using namespace std;
 3 
 4 int main(void){
 5     int n, a[50];
 6     cin >> n;
 7     if(n==0){
 8         cout << 0 << endl;
 9     }else{
10         int k=0;
11         while(n){
12             int t=n%-2;
13             n/=-2;
14             if(t<0){
15                 t+=2;
16                 n+=1;
17             }
18             a[k++]=t;
19         }
20         for(int i=k-1; i>=0; i--){
21             cout << a[i];
22         }
23         cout << endl;
24     }
25     return 0;
26 }      

轉載于:https://www.cnblogs.com/geloutingyu/p/6342569.html