题目大意:输入一个十进制数,将该数转化为-2进制
首先用例子确定一下思路
输入7
7 / -2 = <-3.5> -3余1
-3/ -2 = <1.5> 2余1
2 / -2 = <-1 > -1余0
-1/-2 = <0.5> 1余1
1/-2 = <-0.5> 0余1
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
using namespace std;
#define MAXD 10000
int main(){
int T;
scanf("%d",&T);
for(int Case = 1;Case <= T; Case++){
int array[MAXD];
int L = 0;
double x;
scanf("%lf",&x);
while(x != 0){
long long int t = (long long int)(x);
if(t < 0) t = -t;
array[L++] = t % 2;
x = ceil(x / -2);
}
printf("Case #%d: ",Case);
if(L == 0) printf("0");
else
for(int i = L -1; i >= 0 ;i--)
printf("%d",array[i]);
printf("\n");
}
return 0;
}