Problem SMPSEQ3 - Fun with Sequences - SPOJ

0

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

The proposed problem is as follows: "You get an ordered sequence of n integers S = s1, s2, ..., sn, and an ordered sequence of integers m Q = q1, q2, ..., qm. belong to Q. "

I'm trying to do it in C ++ and using vectors. But I'm bumping into an infinite loop that I'm not sure how to handle.

And I still have not thought about how to compare the vectors and display the ones that are present only in the S sequence.

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'm grateful for the attention.

    
asked by anonymous 07.03.2018 / 15:42

1 answer

0

It took me a while to realize, but I found the "infinite loop"

In this case:

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

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

Infinite loop printing occurs because there is a cin waiting for an input that never occurs.

    
07.03.2018 / 19:54