uva 11121(-2进制)

报告参考:http://www.cnblogs.com/scau20110726/archive/2012/12/21/2828420.html

代码如下:

 1 /**************************************************
 2  * Author     : xiaohao Z
 3  * Blog     : http://www.cnblogs.com/shu-xiaohao/
 4  * Last modified : 2014-03-27 23:04
 5  * Filename     : uva_11121.cpp
 6  * Description     : 
 7  * ************************************************/
 8 
 9 #include <iostream>
10 #include <cstdio>
11 #include <cstring>
12 #include <cstdlib>
13 #include <cmath>
14 #include <algorithm>
15 #include <queue>
16 #include <stack>
17 #include <vector>
18 #include <set>
19 #include <map>
20 #define MP(a, b) make_pair(a, b)
21 #define PB(a) push_back(a)
22 
23 using namespace std;
24 typedef long long ll;
25 typedef pair<int, int> pii;
26 typedef pair<unsigned int,unsigned int> puu;
27 typedef pair<int, double> pid;
28 typedef pair<ll, int> pli;
29 typedef pair<int, ll> pil;
30 
31 const int INF = 0x3f3f3f3f;
32 const double eps = 1E-6;
33 const int LEN = 1001;
34 int kase = 1, f[LEN], t;
35 
36 void solve(int n){
37     t = 0;
38     while(n){
39         int tmp = n%(-2);
40         f[t++] = abs(tmp);
41         n/=(-2);
42         if(tmp == -1) n++;
43     }
44 }
45 
46 int main()
47 {
48 //    freopen("in.txt", "r", stdin);
49 
50     int T, n;
51     cin >> T;
52     while(T--){
53         cin >> n;
54         cout << "Case #" << kase++ << ": ";
55         solve(n);
56         if(t == 0) cout << 0;
57         for(int i=t-1; i>=0; i--) cout << f[i];
58         cout << endl;
59     }
60     return 0;
61 }
View Code
原文地址:https://www.cnblogs.com/shu-xiaohao/p/3629717.html