- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
奇怪的是我找不到任何问题,但它只是崩溃了。在代码第 22 行,错误发生的地方。当输入一对数字的第二行时,程序崩溃。
#include <iostream>
#include <queue>
using namespace std;
int main(){
int t;
cin>>t;
int n, e, s, u, v;
int* martix = nullptr, *mask = nullptr;
for(int i = 0; i<t; i++){
cin>>n>>e>>s;
martix = new int[n*n];
mask = new int[n];
for(int i = 0; i<n*n; i++){
mask[i] = 0;
for(int j = 0; j<n; j++){
martix[i*n+j] = 0;
}
}
while (e--){
cin>>u>>v;//when inputing this two int variable in second time, program crashes
martix[u*n+v] = 1;
martix[v*n+u] = 1;
}
cout<<s<<' ';
mask[s] = 1;
queue<int> q;
q.push(s);
while(!q.empty()){
s = q.front();
q.pop();
for(int i=0; i<n; i++){
if(martix[s*n+i]!=0&&mask[i]!=1){
cout<<i<<' ';
q.push(i);
mask[i] = 1;
}
}
}
cout<<endl;
delete [] mask;
delete [] martix;
}
return 0;
}
测试用例:
1
6 7 0
0 3
0 4
1 4
1 5
2 3
2 4
3 5
预期输出:
0 3 4 2 5 1
最佳答案
i
有可能跑到 n * n - 1
,j
跑到 n - 1
此时(及之前),用作数组索引的表达式 i * n + j
超出了数组边界。这是未定义的行为,并且正在(有帮助地)表现为崩溃。
您的意思是只运行 i
直到 n
,而不是正方形吗?
请注意,如果您在裸数组上使用 std::vector
和 .at
方法而不是 []
,则 C++运行时会抛出一个异常,这对诊断和修复来说是微不足道的。
关于c++ - 辛>>一>>乙; C++ 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59356089/
所以,我目前正在编写一个行编辑器,作为一个关于 I/O、编写文件等的学习项目。它是用 C++ 编写的,我目前正在尝试写入用户选择的文件。我实现了 CLI 参数,但我目前不知道如何在程序中实现指定要写入
如果我通过使用 getline( cin, myStr ); 获得一些值,则在用户输入的信息之后打印换行符 - 逻辑上当他按下回车键时: Please enter something: ABC \n
我一直在 visual studio 2012 控制台模式下处理一个 C++ 项目,我一直在使用 cin 函数时遇到这个奇怪的持续性错误。 在 >>> 下,我得到一条红线,程序告诉我没有运算符匹配这些
奇怪的是我找不到任何问题,但它只是崩溃了。在代码第 22 行,错误发生的地方。当输入一对数字的第二行时,程序崩溃。 #include #include using namespace std; i
我是一名优秀的程序员,十分优秀!