输入-1后在VSCode中冻结输出屏幕



这个节目是关于最长子序列的。我试图输入,直到-1出现,但输出控制台在我输入-1后被挂起。我也试过在在线编译器上运行它……但仍无解

下面是VSCode

中的输出截图
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int> v;
unordered_set<int> s;
int count, res = 0;
cout << "Enter the elements of the array: ";
while (1)
{
cin >> count;
if (count == -1)
break;
v.push_back(count);
s.insert(count);
}
for (int i = 0; i != v.size(); i++)
{
if (s.find(v.at(i) - 1) != s.end())
{
count = 1;
while (s.find(v[i] + 1) != s.end())
count++;
res = max(res, count);
}
}
cout << res;
return 0;
}

我已经在我的两个编译器中编译了你的代码并更改了它的头文件,它对我来说工作得很好

PS我用#include<vector>#include <unordered_set>改变了#include <bits/stdc++.h>头文件。此外,我在if语句中添加了{ }大括号,因为如果我们不使用循环和条件语句的大括号,有时编译器和编辑器会感到困惑。

这是你的代码

#include <iostream>
#include<vector>
#include <unordered_set>
using namespace std;
int main()
{
vector<int> v;
unordered_set<int> s;
int count, res = 0;
cout << "Enter the elements of the array: ";
while (1)
{
cin >> count;
if (count == -1)
{
break;
v.push_back(count);
s.insert(count);
}
}
for (int i = 0; i != v.size(); i++)
{
if (s.find(v.at(i) - 1) != s.end())
{
count = 1;
while (s.find(v[i] + 1) != s.end())
count++;
res = max(res, count);
}
}
cout << res;
return 0;
}

最新更新