2009年2月7日 星期六

Q446: Kibbles `n' Bits `n' Bits `n' Bits

// Run time: 0.000
#include <stdio.h>
void print(int num);

int main()
{
int n;
int a, b;
char op;

scanf("%d", &n);
while (n--)
{
scanf("%X %c %X", &a, &op, &b);
print(a);
printf(" %c ", op);
print(b);
printf(" = ");
if (op == '+')
printf("%d\n", a+b);
else
printf("%d\n", a-b);

}
}

void print(int num)
{
char arr[15];
int mod;
int i;

for ( i = 0; i < 13; i++)
arr[i] = '0';

i = 0;
if (!num)
{
printf("%013d", num);
return;
}

while (num)
{
mod = num % 16;
switch (mod)
{
case 0: arr[i] = arr[i+1] = arr[i+2] = arr[i+3] = '0';
break;
case 1: arr[i+3] = arr[i+2] = arr[i+1] = '0', arr[i] = '1';
break;
case 2: arr[i+3] = arr[i+2] = arr[i] = '0', arr[i+1] = '1';
break;
case 3: arr[i+3] = arr[i+2] = '0', arr[i+1] = arr[i] = '1';
break;
case 4: arr[i+3] = arr[i+1] = arr[i] = '0', arr[i+2] = '1';
break;
case 5: arr[i+3] = arr[i+1] = '0', arr[i+2] = arr[i] = '1';
break;
case 6: arr[i+3] = arr[i] = '0', arr[i+2] = arr[i+1] = '1';
break;
case 7: arr[i+3] = '0', arr[i+2] = arr[i+1] = arr[i] = '1';
break;
case 8: arr[i+3] = '1', arr[i+2] = arr[i+1] = arr[i] = '0';
break;
case 9: arr[i+3] = arr[i] = '1', arr[i+2] = arr[i+1] = '0';
break;
case 10: arr[i+3] = arr[i+1] = '1', arr[i+2] = arr[i] = '0';
break;
case 11: arr[i+3] = arr[i+1] = arr[i] = '1', arr[i+2] = '0';
break;
case 12: arr[i+3] = arr[i+2] = '1', arr[i+1] = arr[i] = '0';
break;
case 13: arr[i+3] = arr[i+2] = arr[i] = '1', arr[i+1] = '0';
break;
case 14: arr[i+3] = arr[i+2] = arr[i+1] = '1', arr[i] = '0';
break;
case 15: arr[i+3] = arr[i+2] = arr[i+1] = arr[i] = '1';
break;
}
num /= 16;
i += 4;
}
for ( i = 12; i >= 0; i--)
printf("%c", arr[i]);
}

沒有留言: