CODE:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int a[1000],b[1000],sum[1000]={0}; /*开大几位,避免越界*/
int flag=0;
int dismember(char array[],int length)
{
int i,j,t;
if(length%4)
{
t=4-length%4;
for(i=t+length-1;i>=t;i--)
array[i]=array[i-t];
length+=t;
for(i=0;i<t;i++) array[i]='0';
}
for(i=0,j=0;i<length;i+=4,j++)
{
if(flag==0)
a[j]=(array[i]-'0')*1000+(array[i+1]-'0')*100+(array[i+2]-'0')*10+array[i+3]-'0';
if(flag==1)
b[j]=(array[i]-'0')*1000+(array[i+1]-'0')*100+(array[i+2]-'0')*10+array[i+3]-'0';
}
length=j;
flag=1;
return length;
}
int main()
{
int i,j,t;
int a_length,b_length;
char temp[1000];
gets(temp);
a_length=strlen(temp);
a_length=dismember(temp,a_length);
gets(temp);
b_length=strlen(temp);
b_length=dismember(temp,b_length);
for(i=a_length-1;i>=0;i--)
for(j=b_length-1;j>=0;j--)
{
t=i+j; /*临时计算,最多减少7500次计算*/
sum[t]+=a[i]*b[j];
sum[t-1]+=sum[t]/10000; /*使用万进制,计算速度加快4倍*/
sum[t]=sum[t]%10000; /*使用万进制,计算速度加快4倍*/
}
while(!sum[i]) i++;
printf("%d",sum[i]);
i++;
for(;i<a_length+b_length-1;i++)
if(sum[i]>1000) printf("%d",sum[i]);
else
if(sum[i]>100) printf("0%d",sum[i]);
else
if(sum[i]>10) printf("00%d",sum[i]);
else
printf("000%d",sum[i]);
system("pause");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int a[1000],b[1000],sum[1000]={0}; /*开大几位,避免越界*/
int flag=0;
int dismember(char array[],int length)
{
int i,j,t;
if(length%4)
{
t=4-length%4;
for(i=t+length-1;i>=t;i--)
array[i]=array[i-t];
length+=t;
for(i=0;i<t;i++) array[i]='0';
}
for(i=0,j=0;i<length;i+=4,j++)
{
if(flag==0)
a[j]=(array[i]-'0')*1000+(array[i+1]-'0')*100+(array[i+2]-'0')*10+array[i+3]-'0';
if(flag==1)
b[j]=(array[i]-'0')*1000+(array[i+1]-'0')*100+(array[i+2]-'0')*10+array[i+3]-'0';
}
length=j;
flag=1;
return length;
}
int main()
{
int i,j,t;
int a_length,b_length;
char temp[1000];
gets(temp);
a_length=strlen(temp);
a_length=dismember(temp,a_length);
gets(temp);
b_length=strlen(temp);
b_length=dismember(temp,b_length);
for(i=a_length-1;i>=0;i--)
for(j=b_length-1;j>=0;j--)
{
t=i+j; /*临时计算,最多减少7500次计算*/
sum[t]+=a[i]*b[j];
sum[t-1]+=sum[t]/10000; /*使用万进制,计算速度加快4倍*/
sum[t]=sum[t]%10000; /*使用万进制,计算速度加快4倍*/
}
while(!sum[i]) i++;
printf("%d",sum[i]);
i++;
for(;i<a_length+b_length-1;i++)
if(sum[i]>1000) printf("%d",sum[i]);
else
if(sum[i]>100) printf("0%d",sum[i]);
else
if(sum[i]>10) printf("00%d",sum[i]);
else
printf("000%d",sum[i]);
system("pause");
return 0;
}
欢迎来到我的博客:http://blog..cn/noisunyuhong