#include <iostream>
using namespace std;
void Warshall()
{
int n, ** p, judge;
cout << "*使用Warshall算法求传递闭包*" << endl;
cout << "请输入顶点个数n:";
cin >> n;
p = new int* [n];
for (int i = 0;i < n;i++)
{
p[i] = new int[n];
}
cout << "请说明以下两节点之间是否直接可达。直接可达请输入1,否则输入0。" << endl; //输入矩阵
for (int i = 0;i < n;i++)
{
for (int j = 0;j < n;j++)
{
cout << "第【" << i + 1 << "】个节点到第【" << j + 1 << "】个节点。请输入0或1:";
cin >> judge;
while (judge < 0 || judge>1) //输入错误提示
{
cout << "【输出有误】请重新输入0或1:" ;
cin >> judge;
}
p[i][j] = judge;
}
}
cout << "--------------------------------------------" << endl;
cout << "邻接矩阵如下:" << endl; //输出邻接矩阵
for (int i = 0;i < n;i++)
{
for (int j = 0;j < n;j++)
{
cout << p[i][j] << " ";
}cout << endl;
}
cout << "--------------------------------------------" << endl;
for (int i = 0;i < n;i++) //运用Warshall算法
{
for (int j = 0;j < n;j++)
{
if (p[j][i] == 1)
{
for (int k = 0;k < n;k++)
{
p[j][k] += p[i][k];
if (p[j][k] > 1)
p[j][k] = 1;
}
}
}
}
cout << "传递闭包矩阵如下:" << endl; //输出传递闭包矩阵
for (int i = 0;i < n;i++)
{
for (int j = 0;j < n;j++)
{
cout << p[i][j] << " ";
}cout << endl;
}
for (int i = 0; i < n; i++) //释放内存
{
delete[] p[i], p[i] = nullptr;
}
delete[]p, p = nullptr;
}
int main()
{
Warshall();
system("pause");
return 0;
}
有个问题:输入错误提示用while(judge!=0||judge!=0) 好像不行。why