#include<stdio.h> #include<string.h> int main() { char a[20][5]={"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"}; char b[20]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; long test; scanf("%ld",&test); while(test!=0) { char d[100]="0",c[20],e[100]="0",ch,f[100]; int i,j,k,l,m,n,sum1=0,sum2=0,sum3; scanf("%s %c %s",&c,&ch,&f); if(strlen(c)==1) { strcat(e,"00000000"); for(i=0;i<16;i++) { if(b[i]==c[0]) { strcat(e,a[i]); break; } } } else if(strlen(c)==2) { strcat(e,"0000"); for(j=0;j<2;j++) { for(i=0;i<16;i++) { if(c[j]==b[i]) { strcat(e,a[i]); break; } } } } else { for(j=0;j<3;j++) { for(i=0;i<16;i++) { if(c[j]==b[i]) { strcat(e,a[i]); break; } } } } if(strlen(f)==1) { strcat(d,"00000000"); for(i=0;i<16;i++) { if(b[i]==f[0]) { strcat(d,a[i]); break; } } } else if(strlen(f)==2) { strcat(d,"0000"); for(j=0;j<2;j++) { for(i=0;i<16;i++) { if(f[j]==b[i]) { strcat(d,a[i]); break; } } } } else { for(j=0;j<3;j++) { for(i=0;i<16;i++) { if(f[j]==b[i]) { strcat(d,a[i]); break; } } } } for(i=0;i<strlen(e);i++) { sum1=sum1*2+(e[i]-48); } for(i=0;i<strlen(d);i++) { sum2=sum2*2+(d[i]-'0'); } if(ch=='+')sum3=sum1+sum2; else sum3=sum1-sum2; printf("%s %c %s = %d\n",e,ch,d,sum3); test--; } return 0; }
Sunday, August 17, 2014
uva : 446 - Kibbles "n" Bits "n" Bits "n" Bits
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment