#include<iostream> #include<string> using namespace std; int main() { int t; cin >> t; while (t--) { int i, j, kind, VertexNum, nfe, AdjMatrix[99][99]; string Vertex[100]; cin >> kind >> VertexNum; //输入顶点数目 for (i = 1; i <= VertexNum; i++) cin >> Vertex[i]; //输入顶点 cin >> nfe; for (int m = 1; m <= nfe; m++) { string judge1, judge2; int k = 0, p = 0; cin >> judge1 >> judge2; for (int i = 1; i <= VertexNum; i++) { if (judge1 == Vertex[i]) { k = i; break; } } for (int i = 1; i <= VertexNum; i++) { if (judge2 == Vertex[i]) { p = i; break; } } if (kind == 'D') AdjMatrix[k][p] = 1; else { AdjMatrix[k][p] = 1; AdjMatrix[p][k] = 1; } } for (int i = 1; i <= VertexNum; i++) { for (int j = 1; j <= VertexNum; j++) { if (!AdjMatrix[i][j]) { AdjMatrix[i][j] = 0; } } } for (int i = 1; i <= VertexNum; i++) { for (int j = 1; j < VertexNum; j++) { cout << AdjMatrix[i][j] << ' '; } cout << AdjMatrix[i][VertexNum] << endl; } for (int i = 1; i <= VertexNum; i++) { if (kind == 'D') { int intcheck1, outcheck2, total; for (int j = 1; j <= VertexNum; j++) { if (AdjMatrix[i][j] == 1) intcheck1++; if (AdjMatrix[j][i] == 1) outcheck2++; } total = intcheck1 + outcheck2; cout << Vertex[i] << ": " << intcheck1 << ' ' << outcheck2 << ' ' << total << ' ' << endl; } else { int total2; for (int j = 1; j <= VertexNum; j++) { if (AdjMatrix[i][j] == 1) total2++; } cout << Vertex[i] << ": " << total2 << endl; } } } return 0; }问题在哪
时间: 2024-04-27 13:21:03 浏览: 59
C++的iostream标准库介绍以及对左移与右移运算符的重载.pdf
这段代码可能存在一些问题,具体如下:
1. 在读入kind时,应该使用字符类型char,而不是int类型。
2. 在读入邻接矩阵时,如果输入的是无向图,应该同时赋值AdjMatrix[p][k] = 1,而不是只赋值AdjMatrix[k][p] = 1。因为无向图的邻接矩阵是对称的,即AdjMatrix[k][p] = AdjMatrix[p][k] = 1。
3. 在计算每个顶点的出度、入度和度数时,应该在每次循环前将intcheck1和outcheck2的值初始化为0,否则会出现计数错误。
阅读全文