📄 warshall.cpp.bak
字号:
#include < iostream >
using namespace std;
int main()
{
int M[4][4];
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int test;
while( a != 4 ){
a = 0;
cout<<"Enter the four elements on the first line of the matrix: "<<endl;
do{
cin>>M[0][a];
test = M[0][a];
a++;
}while( a <= 3 && (test == 1 || test == 0 ) );
if( a != 4 ){
cout<<"Wrong number has been entered."<<endl;
}
}
while( b != 4 ){
b = 0;
cout<<"Enter the four elements on the second line of the matrix: "<<endl;
do{
cin>>M[1][b];
test = M[1][b];
b++;
}while( b <= 3 && (test == 1 || test == 0 ) );
if( b != 4 ){
cout<<"Wrong number has been entered."<<endl;
}
}
while( c != 4 ){
c = 0;
cout<<"Enter the four elements on the third line of the matrix: "<<endl;
do{
cin>>M[2][c];
test = M[2][c];
c++;
}while( c <= 3 && (test == 1 || test == 0 ) );
if( c != 4 ){
cout<<"Wrong number has been entered."<<endl;
}
}
while( d != 4 ){
d = 0;
cout<<"Enter the four elements on the forth line of the matrix: "<<endl;
do{
cin>>M[3][d];
test = M[3][d];
d++;
}while( d <= 3 && (test == 1 || test == 0 ) );
if( d != 4 ){
cout<<"Wrong number has been entered."<<endl;
}
}
cout<<"The matrix entered is :"<<endl;
for( int s = 0; s < 4; s++ ){
for( int t = 0; t < 4; t++)
cout<<" "<<M[s][t];
cout<<endl;
}
for(int k = 0; k < 4 ; k++){
for(int i = 0; i < 4; i++ ){
for(int j = 0; j < 4 ; j++){
M[i][j]= ( ( M[i][j] + M[i][k] * M[k][j] ) >= 1 ? 1 : 0 );
/*将所求路径分成两种情况,第一类是M( i , j )已经等于1,此时M( i , j )必等于1
第二类是M( i , k ) = 1 并且M( k , j ) = 1 ,此时M( i , j )等于1
且循环顺序不能乱*/
}
}
}
cout<<"The result is :"<<endl;
for( int x = 0; x < 4; x++ ){
for( int y = 0; y < 4; y++)
cout<<" "<<M[x][y];
cout<<endl;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -