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