这是n^2的大数相乘,本想拿到OJ上跑跑的,但是看了看人家要求的是FFT,姿势还不够。。。话说我测试了几组数据都对了。。有不对欢迎指出。。。
#include <stdio.h>
#include <string.h>
const int maxn = 5000+10;
char str1[maxn], str2[maxn];
int temp[maxn];
int main()
{
while(1){
int sum, t;
char *p=str1, *q=str2;
memset(str1,0,sizeof(str1));
memset(str2,0,sizeof(str2));
memset(temp,0,sizeof(temp));
scanf("%s%s",str1, str2);
int len1 = strlen(str1);
int len2 = strlen(str2);
if(len1<=len2){
t = len1;
len1 = len2;
len2 = t;
p = str2;
q = str1;
}
for(int j=len2-1; j>=0; j--){
sum = len2 - j -1;
for(int i=len1-1; i>=0; i--){
temp[sum] += (p[i] - '0') * (q[j] - '0');
temp[sum+1] += temp[sum]/10;
temp[sum] %= 10;
sum++;
}
}
if(temp[sum]!=0) printf("%d",temp[sum]);
for(int i=(sum-1); i>=0; i--)
printf("%d",temp[i]);
printf("\n");
}
return 0;
}
742

被折叠的 条评论
为什么被折叠?



