#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 10:21:03 浏览: 13
这段代码可能存在一些问题,具体如下:
1. 在读入kind时,应该使用字符类型char,而不是int类型。
2. 在读入邻接矩阵时,如果输入的是无向图,应该同时赋值AdjMatrix[p][k] = 1,而不是只赋值AdjMatrix[k][p] = 1。因为无向图的邻接矩阵是对称的,即AdjMatrix[k][p] = AdjMatrix[p][k] = 1。
3. 在计算每个顶点的出度、入度和度数时,应该在每次循环前将intcheck1和outcheck2的值初始化为0,否则会出现计数错误。
相关问题
#include <iostream> #include <algorithm> #include <string> #include <cstdio> #include <cmath> #include <set> #include <map> using namespace std; //const int N = 1e5 + 10; //typedef long long ll; int main() { int t; cin >> t; while (t--) { int n; set<string>a; string s; cin >> n >> s; int t = 0; for (int i = 0; i < s.size(); i++) { t = s.find(s[t]); a.insert(s.substr(i, 1)); } for (int i = 0; i < a.size(); i++) cout << a[i]; cout << endl; } return 0; }有什么问题
这段代码存在一些问题:
1. 在第二个 for 循环中,使用了下标访问 set 容器,这是错误的,因为 set 容器不支持下标访问。
2. 在第二个 for 循环中,使用了 a.size(),这也是错误的,因为 set 容器没有 size() 方法,应该使用 a.size()。
3. 在第一个 for 循环中,应该使用 s.substr(i, 1) 而不是 s.substr(0, t+1),因为后者会将 s 的前 t+1 个字符作为子串,而不是从 i 开始的一个字符。
4. 在第一个 for 循环中,应该将 a.insert() 放在 t = s.find(s[t]) 的后面,因为插入的是 s.substr(i, 1),插入顺序应该是从前往后。
修正后的代码如下:
```c++
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
set<string> a;
string s;
cin >> n >> s;
int t = 0;
for (int i = 0; i < s.size(); i++) {
a.insert(s.substr(i, 1));
t = s.find(s[t]);
}
for (auto it = a.begin(); it != a.end(); it++) cout << *it;
cout << endl;
}
return 0;
}
```
优化这段代码#include <iostream> #include <vector> #include <string> using namespace std; int main() { string s; s.reserve(100); char c; while (cin >> c) { s.push_back(c); } cout << "s===" << s << endl; return 0; }
这段代码的主要问题在于使用了大量的string操作,如果输入的数据量很大,会导致效率较低。可以使用字符数组来代替string,同时减少vector的使用,代码如下:
```c++
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char s[100];
int len = 0;
char c;
while (cin >> c) {
s[len++] = c;
}
s[len] = '\0';
cout << "s===" << s << endl;
return 0;
}
```
这里使用了字符数组s来存储输入的字符,同时使用一个变量len来记录当前字符数组中已经存储的字符数。当输入一个字符时,将其存入字符数组s中,并将len加1。最后,将字符数组s的最后一个字符设置为'\0',以便输出时正确显示字符串。这样可以避免使用string的大量操作,从而提高了效率。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)