Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
923 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...