第二类——数学题

   有时很容易栽在一些边界条件。考虑全面的习惯要在平时的训练中多加注意。

2008年 又一版A+B(http://acm.hdu.edu.cn/showproblem.php?pid=1877)
#include<stdio.h> #include<string.h> int a,b; int x[100],y[100]; //原来是32,放上去就报错? int xi,yi; //the number of x or y void ChangeSystem(int a,int n,int x[],int &xi) { xi = 0; memset(x,0,100); if(a == 0) {xi=1;return;} while(a!=0) { x[xi++] = a%n; a/=n; } } int main() { int n; //number systems while(scanf("%d",&n)&&n!=0) { scanf("%d%d",&a,&b); ChangeSystem(a,n,x,xi); ChangeSystem(b,n,y,yi); int min = xi;int *z=y; int max = yi; if(xi>yi) { min = yi; z = x; max = xi; } for(int i=0;i<max;i++) { if(x[i]+y[i]>=n) { z[i]=(x[i]+y[i])%n; z[i+1]++; } else { z[i] = x[i]+y[i]; } } if(z[max]!=0) { printf("%d",z[max]); } for(int j=max-1;j>=0;j--) { printf("%d",z[j]); } printf("/n"); } return 0; }

 

   总结:原来错在没有考虑好0这个边界,以及进制转换后的数组大小的最大值的设定(这点明天再想想看为什么不是32)。看给的答案,它说A+B的和不会超出INT型的范围,但我觉得,A,B虽然没有超出int型的范围,但A+B超出int型的范围是很有可能的。这点上有点疑问...

你可能感兴趣的:(第二类——数学题)