大数除法。。。赶得好急,借鉴牛人的写法了
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MaxLen 200
int SubStract(int *p1,int *p2,int len1,int len2)
{
int i;
if(len1<len2)
return -1;
if(len1==len2)
{
for(i=len1-1;i>=0;i--)
{
if(p1[i]>p2[i])
break;
else if(p1[i]<p2[i])
return -1;
}
}
for(i=0;i<=len1-1;i++)
{
p1[i]-=p2[i];
if(p1[i]<0)
{
p1[i]+=10;
p1[i+1]--;
}
}
for(i=len1-1;i>=0;i--)
if(p1[i])
return (i+1);
return 0;
}
int main()
{
int n,k,i,j;
int len1,len2;
int nTimes;
int nTemp;
int num_a[MaxLen];
int num_b[MaxLen];
int num_c[MaxLen];
char str1[MaxLen+1];
char str2[MaxLen+1];
scanf("%d",&n);
while(n--)
{
scanf("%s",str1);
scanf("%s",str2);
memset(num_a,0,sizeof(num_a));
memset(num_b,0,sizeof(num_b));
memset(num_c,0,sizeof(num_c));
len1=strlen(str1);
len2=strlen(str2);
for(j=0,i=len1-1;i>=0;j++,i--)
num_a[j]=str1[i]-'0';
for(j=0,i=len2-1;i>=0;j++,i--)
num_b[j]=str2[i]-'0';
if(len1<len2)
{
printf("0\n");
continue;
}
nTimes=len1-len2;
for(i=len1-1;i>=0;i--)
{
if(i>=nTimes)
num_b[i]=num_b[i-nTimes];
else
num_b[i]=0;
}
len2=len1;
for(j=0;j<=nTimes;j++)
{
while((nTemp=SubStract(num_a,num_b+j,len1,len2-j))>=0)
{
len1=nTemp;
num_c[nTimes-j]++;
}
}
for(i=nTimes;num_c[i]==0&&i>=0;i--); //原来是i=MaxLen-1,感觉没必要-.-
if(i>=0)
for(;i>=0;i--)
printf("%d",num_c[i]);
else
printf("0");
printf("\n");
}
return 0;
}