TÓPICO

Wrong Answer 5%????

vhmartins4 perguntou 5 years ago

Alguém sabe onde está o erro???

#include <iostream>

using namespace std;

void ordena(int vetor[], int tamanho);
void troca(int *a, int *b);

int main (){

    int vetor[1000], n, q, p;
    while ( cin >> n >> q ){
        for ( int i = 0 ; i < n ; i ++ )
            cin >> vetor[i];

        ordena(vetor, n);

        for ( int i = 1 ; i <= q ; i++ ){
            cin >> p;
            cout << vetor[p-1] << endl;
        }
    }
    return 0;
}

void ordena(int vetor[], int tamanho){
    bool trocou;
    do {
        trocou = false;
        for (int i = 0; i < tamanho; i++) {
            if (*(vetor+i) < *(vetor+i+1)) {
                troca(vetor+i, vetor+i+1);
                trocou = true;
            }
        }
    } while (trocou);
}

void troca(int *a, int *b){
    int aux = *a;
    *a = *b;
    *b = aux;
}

Lembre de não publicar soluções. Sua publicação pode ser revisada por nossos moderadores.

  • feodorv respondido 5 years ago

            for (int i = 0; i < tamanho; i++) {
                if (*(vetor+i) < *(vetor+i+1)) {

    Hm. If i becomes tamanho-1 you are addressing

     *(vetor+tamanho)

    which lays out of array data.