2008 TOI 研習營初選 TOI2008 2. 大數運算
sagit的程式碼@@
/***********************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int Check(int a[],int b[],int lb,int lc)
{
int i;
for (i=lb-1; i>=0; i--)
{
if (a[i+lc]<b[i]) return 0;
if (a[i+lc]>b[i]) return 1;
}
return 1;
}
main()
{
int a,b,c,d;
char x[501],y[501],z,max,flag;
while(scanf("%s %c %s",x,&z,y)==3)
{
int temp[1100]={0};
if(z=='*')
{
int m=strlen(x),n=strlen(y);
for(a=0,c=m-1;c>=0;a++,c--)
for(b=0,d=n-1;d>=0;b++,d--)
temp[a+b]=temp[a+b]+(x[c]-48)*(y[d]-48);
for(a=0;a<n+m+1;a++)
if(temp[a]>=10)
{
temp[a+1]=temp[a+1]+temp[a]/10;
temp[a]=temp[a]%10;
}
for(a=m+n+1;a>=0;a--)
if(temp[a]!=0)
{
for(b=a;b>=0;b--)
printf("%d",temp[b]);
break;
}
}
if(z=='/')
{
int n=strlen(x),m=strlen(y),xx[501]={0},yy[501]={0};
for(a=0,c=n-1;c>=0;a++,c--)
xx[a]=x[c]-48;
for(a=0,c=m-1;c>=0;a++,c--)
yy[a]=y[c]-48;
int la=n+1,lb=m+1,i,j;
for (i=la-lb; i>=0; i--)
{
while(Check(xx,yy,lb,i))
{
temp[i]++;
for(j=0;j<lb;j++)
{
xx[i+j]-=yy[j];
if(xx[i+j]<0)
{
xx[i+j]+=10;
xx[i+j+1]--;
}
}
}
}
int flag=1;
for(a=n+m;a>=0;a--)
if(temp[a]!=0)
{
for(b=a;b>=0;b--)
printf("%d",temp[b]);
flag=0;
break;
}
if(flag==1) printf("0\n");
}
printf("\n");
}
return 0;
}