利用%x將十六進位制數字直接輸入,接著利用bit運算輸出二進位格式,至於加減結果直接對數值做加法或減法即可。
C++(0.000)
/*******************************************************/
/* UVa 446 Kibbles "n" Bits "n" Bits "n" Bits */
/* Author: Maplewing [at] knightzone.studio */
/* Version: 2015/12/02 */
/*******************************************************/
#include <iostream>
#include <cstdio>
using namespace std;
void printBinary(int n){
for( int i = 12 ; i >= 0 ; --i ){
printf("%d", (n & (1 << i)) >> i );
}
}
int main(){
int N;
while( scanf("%d", &N) != EOF ){
int a, b;
char op;
for( int i = 0 ; i < N ; ++i ){
scanf("%x %c %x", &a, &op, &b);
printBinary(a);
printf(" %c ", op);
printBinary(b);
if( op == '+' ){
printf(" = %d\n", a+b );
}
else if( op == '-' ){
printf(" = %d\n", a-b );
}
}
}
return 0;
}