vendredi 6 mars 2020

my code only calls my function once instead of k times

so I have this code that has a function on it that is called k times but my code only runs it one times

example if you put this manually

2

1

1

5

1 3 5 2 2

it will not let you finish and will throw 1 instead of 1 and 3

#include <bits/stdc++.h>
using namespace std;

int funcion(int n) {
    int arr[n], l;
    int c = -1;
    cin >> l;
    arr[0] = l;
    for (int i = 1; i<n; i++) {
        cin >> l;
        arr[i] = arr[i - 1] + l;
    }

    for (int i = 0; i<n; i++) {
        if (arr[n - 1] - arr[i] == arr[i - 1]) {
            c = i + 1;
            i = n + 500;
        }

    }


    return c;
}
int main() {

    int k, n;
    cin >> k;
    for (int i = 0; i<k; i++) {
        cin >> n;
        cout << funcion(n) << endl;
    }
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire