http://acm.hdu.edu.cn/showproblem.php?pid=2057
十六进制转成十进制,加减后再转成十六进制输出。。。。。转成的十进制太大,用__int64
View Code
1 #include<stdio.h> 2 #include<string.h> 3 __int64 zhuan(char *s) 4 { 5 __int64 i=0,sum=0; 6 if(s[i]=='-'||s[i]=='+') 7 i++; 8 while(s[i]) 9 { 10 if(s[i]>='0'&&s[i]<='9') 11 sum=(s[i]-'0')+sum*16; 12 else sum=(s[i]-'A'+10)+sum*16; 13 i++; 14 } 15 if(s[0]=='-') 16 return (-sum); 17 return sum; 18 } 19 void hui(__int64 n,char *s) 20 { 21 __int64 i=0,j=0,a[100],k; 22 while(n/16) 23 { 24 a[i++]=n%16; 25 n/=16; 26 } 27 a[i++]=n; 28 while(i--) 29 { 30 if(a[i]>9) 31 s[j++]=a[i]-10+'A'; 32 else s[j++]=a[i]+'0'; 33 } 34 s[j]=0; 35 } 36 int main() 37 { 38 __int64 i,j,flag,a,b,sum; 39 char aa[20],bb[20],ans[20]; 40 while(scanf("%s %s",aa,bb)!=EOF) 41 { 42 a=zhuan(aa); 43 b=zhuan(bb); 44 sum=a+b; 45 if(sum>=0) 46 flag=1; 47 else { 48 flag=-1; 49 sum=-sum; 50 } 51 if(flag==-1) 52 printf("-"); 53 hui(sum,ans); 54 printf("%s\n",ans); 55 } 56 return 0; 57 }