添哥 @ 2020-07-15 20:07:25
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char a[2001],b[2001];
int c[4001]={0},lena,lenb,d=0;
cin>>a>>b;
lena=strlen(a)-1;
lenb=strlen(b)-1;
for(int i=0;i<=lena;i++)
{
for(int j=0;j<=lenb;j++)
{
c[i+j]+=(a[i]-'0')*(b[i]-'0')/10;
if(c[i+j]>=10)
{
c[i+j+1]+=c[i+j]%10;
c[i+j]%=10;
}
c[i+j-1]+=(a[i]-'0')*(b[i]-'0')%10;
if(c[i+j-1]>=10)
{
c[i+j]+=c[i+j-1]%10;
c[i+j-1]%=10;
}
}
}
for(int i=lena*lenb;i>=0;i--)
{
if(c[i]>0)
{
d=1;
}
if(d)
{
cout<<c[i];
}
}
return 0;
}
样栗连输出都没有QAQ
by 添哥 @ 2020-07-15 20:16:55
@天命之路 我不是已经赋初值了吗
by WanderingTrader @ 2020-07-15 20:17:40
@添哥 在main函数里的数组,每一个值都是随机的
by 添哥 @ 2020-07-15 20:19:19
@zycany 哦。。。
by 添哥 @ 2020-07-15 20:20:06
@zycany 还是没有输出QAQ
by 0xC00000FD @ 2020-07-15 20:23:33
1 : 非全局变量初值随机
solve :
char a[2001] = { 0 }, b[2001] = { 0 };
int c[4001] = { 0 };
2 : LZ看起来想要去掉前导零, 但是如上的方法会把必要的数中的零去掉
solve:
int len = lena * lenb;
while (len > 0 && !c[len - 1]) len --;
for (int i = len - 1; i >= 0; i --)
putchar(c[i] ^ 48);
3 : 其实您作为一个橙名大佬我怀疑您在钓鱼
by 添哥 @ 2020-07-15 20:29:02
@cyan_lemon 谢巨佬%%%
by 0xC00000FD @ 2020-07-15 20:32:06
补一下
1 :
len = lena + lenb;
2 : LZ你并没有倒序
3 :
c[i+j]+=(a[i]-'0')*(b[i]-'0')/10;
if(c[i+j]>=10)
{
c[i+j+1]+=c[i+j]%10;
c[i+j]%=10;
}
c[i+j-1]+=(a[i]-'0')*(b[i]-'0')%10;
if(c[i+j-1]>=10)
{
c[i+j]+=c[i+j-1]%10;
c[i+j-1]%=10;
}
这段你确定b数组的下标不是j而是i?
by 0xC00000FD @ 2020-07-15 20:36:38
太多问题了(或者想法重合度太低了), 我先全部改好调试好再发给你你看一下
by zhoukangyang @ 2020-07-15 20:41:01
没看懂,我丢我的代码吧。。
#include<bits/stdc++.h>
using namespace std;
struct CP {
double x,y;
CP (double xx = 0, double yy = 0) {
x = xx, y = yy;
}
};
CP operator + (CP a,CP b) {
return CP(a.x+b.x,a.y+b.y);
}
CP operator - (CP a,CP b) {
return CP(a.x-b.x,a.y-b.y);
}
CP operator * (CP a,CP b) {
return CP(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);
}
CP a[3000009],b[3000009];
int n,m,p[3000009],pp[3000009];
char cha[3000009],chb[3000009];
double pi = 3.14159265359;
void fft(CP *f,int len,int flag) {
if(len==1) return;
fft(f,(len>>1),flag),fft(f+(len>>1),(len>>1),flag);
CP ch,now;
now.x=1,now.y=0,ch.x=cos(2*pi/len),ch.y=flag*sin(2*pi/len);
for(int i = 0; i < (len>>1); i++) f[i]=(f[i]+(f[i+len/2]*now)),f[i+len/2]=(f[i]-(f[i+len/2]*now)-(f[i+len/2]*now)),now=now*ch;
}
int main() {
scanf("%s%s",&cha,&chb);
n=strlen(cha),m=strlen(chb);
for(int i = 0; i < n; i++) a[n-i-1].x=cha[i]-48;
for(int i = 0; i < m; i++) b[m-i-1].x=chb[i]-48;
for(n = m+n; n != (n&-n); n += (n&-n));
for(int i = 0; i < n; i++) pp[i]=((pp[i>>1]>>1)|((i&1)*(n>>1)));
for(int i = 0; i < n; i++) if(i<pp[i]) swap(a[i],a[pp[i]]);
for(int i = 0; i < n; i++) if(i<pp[i]) swap(b[i],b[pp[i]]);
fft(a,n,1),fft(b,n,1);
for(int i = 0; i < n; i++) a[i]=a[i]*b[i];
for(int i = 0; i < n; i++) if(i<pp[i]) swap(a[i],a[pp[i]]);
fft(a,n,-1);
for(int i = 0; i < n; i++) p[i]=(int)(a[i].x/n+0.49);
for(int i = 0; i < n; i++) p[i+1]+=p[i]/10,p[i]%=10;
while(!p[n]) --n;
for(int i = n; i >= 0; i--) printf("%d",p[i]);
return 0;
}
by syksykCCC @ 2020-07-15 20:49:36
楼上毒瘤