已知一堆苹果的数量和一个人与另一个人苹果数量的差,求每个人有几个苹果。需要用到高精度。
#include<cstdio> #include<cstring> char x[105],y[105]; int a[105],b[105],c[105]; int l1,l2,i,j,s; int main(){ while(scanf("%s%s",&x,&y)!=EOF){ memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); l1=strlen(x);l2=strlen(y); for (i=0;i<l1;i++) a[l1-i]=x[i]-48; for (i=0;i<l2;i++) b[l2-i]=y[i]-48; for (i=1;i<=l1;i++){ a[i]=a[i]+b[i]+a[i-1]/10; a[i-1]%=10; } s=0; for (i=l1;i;i--){ a[i]+=s*10; s=a[i]&1; a[i]>>=1; } if (!a[l1])l1--; for (i=l1;i;i--) printf("%d",a[i]); printf("\n"); for (i=1;i<=l1;i++){ a[i]=a[i]-b[i]; if (a[i]<0){a[i+1]--;a[i]+=10;} } while(!a[l1])l1--; for (i=l1;i;i--) printf("%d",a[i]); printf("\n"); } }
时间: 2024-10-29 11:19:12