SMPSEQ3 problem - Fun with Sequences - SPOJ

Asked

Viewed 147 times

0

Good morning. I would like a help to create a code that is accepted by SPOJ.

The proposed problem is the following: "You get an ordered sequence of n integers S = S1, s2, ..., Sn and an ordered sequence of integers m Q = Q1, Q2, ..., qm. Please print in ascending order all those that do not belong to Q."

I’m trying to do it in C++ and using vectors. But I’m running into an infinite loop that I can’t handle.

And I still haven’t thought about how to compare the vectors and display those that are only present in sequence S.

Follow the code I’ve made so far:

#include <iostream>
using namespace std;
int main () {
    int S, Q;
    cin >> S;
    int * S_vetor = new int[S];

    for (int i = 0; i < S; i++) {
        cin >> S_vetor[i];
    }
    cin >> Q;
    int * Q_vetor = new int[Q];

    for (int i = 0; i <= Q; i++) {
        cin >> Q_vetor[i];
    }
    return 0;
}

From now on, I am grateful for the attention.

  • what values you are entering for S and Q, when the program runs?

  • I suggest putting text messages before cin - I think that’s your "infinite loop"...

  • Values between -100 and 100. As well, text messages before Cin?

  • Ah, at Codeblocks he’s got the fever...

  • the cin blocks execution. If you have not typed all expected values, it seems that the program has locked... Puts a cout >> "digite o valor de...", for example...

  • But the SPOJ requires that the answer be only to appear equal to their example: Appear the size of the vector, below its elements. Then the size of the other vector, under its elements. Finally, only the integers that are in the first vector and not in the second.

Show 1 more comment

1 answer

0

It took me a while to figure it out, but I found the "infinite loop"

Nessa for:

for (int i = 0; i <= Q; i++)

You’re waiting for an element more than the read, because of the test i <= Q. Remember that your gone starts at zero, so to read Q elements, you need to test i < Q.

The impression of infinite loop is given because there is a cin waiting for an input that never occurs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.