Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
919 views
in Technique[技术] by (71.8m points)

vector - Freezing output screen in VSCode after entering -1

The program is about LONGEST SUBSEQUENCE. I tried to give input until -1 is occurred but the output console is getting hanged after I enter -1. I tried running it on online compilers too...but still no solution.

Here is the output screenshot in 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;
}
question from:https://stackoverflow.com/questions/65876069/freezing-output-screen-in-vscode-after-entering-1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have compiled your code in two of my compiler and changed its header files it works fine for me

PS I changed #include <bits/stdc++.h> header file with #include<vector> and #include <unordered_set>. Moreover I added { } currly braces in if statement because sometimes compilers as well as editors gets confuse if we dont use currly braces with loops and conditional statement.

Here is your code

#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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...