一道简单题,简单的20进制加减法,我这里代码写的不够优美,还是可以有所改进,不过简单题懒得改了。。。
#include <stdio.h> #include <string.h> int invert(char c) { if(c<97) return c-48; else return c-87; } char reinvert(int a) { if(a<10) return a+48; else return a+87; } char add(char a1,char a2,int *c) { int t1,t2,sum; t1=invert(a1); t2=invert(a2); sum=t1+t2+*c; if(sum<20) { *c=0; } else { sum-=20; *c=1; } return reinvert(sum); } void inversion(char *a) { int len=strlen(a); for(int i=0,j=len-1;i<=j;i++,j--) { char c=a[i]; a[i]=a[j]; a[j]=c; } } int main() { char a1[120],a2[120]; while(scanf("%s%s",a1,a2)!=EOF) { inversion(a1); inversion(a2); int len1=strlen(a1),len2=strlen(a2),i; int c=0; for(i=0;i<len1&&i<len2;i++) a1[i]=add(a1[i],a2[i],&c); if(len1>len2) { for(;i<len1;i++) a1[i]=add(a1[i],'0',&c); if(c!=0) a1[i++]='1'; a1[i]='