末影龙写了以下这个程序,发现了一个他无法解决的问题:
#include <cstdio>
inline bool Read(int &x)
{
x=0;
register char ch=getchar();
if(ch=='\n'||ch==EOF)
{
return 0;
}
else
{
while(ch<'0'||ch>'9')
{
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=(x<<3)+(x<<1)+(ch^'0');
ch=getchar();
}
return 1;
}
}
class Matrix
{
private:
static const unsigned long int MATRIX_MAX=6001;
int height,width;
int value[MATRIX_MAX][MATRIX_MAX];
public:
Matrix()
{
height=width=0;
}
Matrix operator*(const Matrix &x)const
{
Matrix result;
result.height=height;
result.width=x.width;
for(int i=1;i<=height;++i)
{
for(int j=1;j<=x.width;++j)
{
result.value[i][j]=0;
for(int k=1;k<=width;++k)
{
result.value[i][j]+=value[i][k]*x.value[k][j];
}
}
}
return result;
}
inline void build(const int x,const int y)
{
height=x;
width=y;
return ;
}
inline void change(const int i,const int j,const int x)
{
value[i][j]=x;
return ;
}
inline int get(const int i,const int j)
{
return value[i][j];
}
inline void output()
{
for(int i=1;i<=height;++i)
{
for(int j=1;j<=width;++j)
{
printf("%d ",value[i][j]);
}
printf("\n");
}
}
};
int x,y,m,n,o,p,i,j,a;
Matrix A,B,C,D;
int main()
{
puts("OK!");
Read(x);
Read(y);
Read(m);
Read(n);
Read(o);
Read(p);
A.build(m,n);
B.build(n,o);
C.build(o,p);
while(Read(i))
{
Read(j);
Read(a);
A.change(i,j,a);
}
while(Read(i))
{
Read(j);
Read(a);
B.change(i,j,a);
}
while(Read(i))
{
Read(j);
Read(a);
C.change(i,j,a);
}
D=A*B*C; //<-----这里
printf("%d\n",D.get(x,y));
return 0;
}
这个程序在本地运行会非正常退出,甚至连主函数的第一句 puts("OK!"); 都不会执行。但当把程序最后的 D=A*B*C; 注释掉之后,程序就可以正常运行了。
或许是 Matrix operator*(const Matrix &x)const 函数出现了问题?但是末影龙实在是太菜了,他看不出来问题在哪里。
我知道这么暴力的矩阵乘法是过不了的,但我只想知道为什么这个程序不能正常运行QWQ