简体   繁体   中英

Getting error on submission for Kattis "OddGnome" problem. I am getting the right output locally, what is wrong with my code? C++

#include <stdio.h>
int main(){ 
    int n, g, i;
    scanf("%d\n", &n);
    while(n--) {
        int l = -1;
        int c = 1;
        scanf("%d", &g);
        while(g--) {
            scanf("%d", &i); 
            if (l == -1) l = i;
            else if (i - 1 != l) break;
            else l++;
            c++;
        }
        fflush(stdin);
        printf("%d\n", c);
    }
} 

I get all the outputs correctly, so I have no idea what could be wrong. On the other hand, Kattis accepts this code below that I found on GitHub, and the outputs are exactly the same as in my code. If anyone can explain to me what is wrong or why Kattis rejects my code I would appreciate it.

#include <bits/stdc++.h>

using namespace std;

int main()
{
    //Initialize n and g, take in n
    int n, g;
    cin >> n;

    //Iterate n times
    while (n--)
    {
        //Take in g, initialize empty vector of size g
        cin >> g;
        vector<int> gnomes(g);

        //Take in all the gnomes
        for (int i = 0; i < g; i++) cin >> gnomes[i];

        //Iterate through without the beginning or end since king won't be there
        for (int i = 1; i < g-1; i++)
        {
            //Must break the order, and if you remove it the gnomes around it should be in order
            if (gnomes[i] < gnomes[i-1] || gnomes[i] > gnomes[i+1] && gnomes[i-1] < gnomes[i+1])
            {
                //Output the 1 based index, so add 1
                cout << i+1 << endl;

                //And exit to the next group
                break;
            }
        }
    }
}

First of all, if you're using C++, use C++, not C!

As pointed out, fflush(stdin) is undefined behavior . Instead, you can use std::getline to chew up the rest of the line.

Other than that, your logic looks fine, even though it's a different approach than the overkill C++ solution. There's no need to check i + 1 or use a vector, as you seem to have deduced.

I suggest using clearer naming and whitespace conventions, though.

Here's a C solution, using scanf to read the rest of the line:

#include <stdio.h>

int main() {
    int n;
    scanf("%d\n", &n);

    for (int i = 0; i < n; i++) {
        int previous = -1;
        int g;
        scanf("%d", &g);

        for (int j = 0; j < g; j++) {
            int current;
            scanf("%d", &current);

            if (previous >= 0 && previous + 1 != current) {
                printf("%d\n", ++j);

                for (; j < g; j++) {
                    scanf("%d", &current); 
                }

                break;
            }

            previous = current;
        }
    }
} 

Here's a C++ solution that uses bits/stdc++.h and using namespace std; , which are common idioms in competitive programming but should never be used in any application code.

#include <bits/stdc++.h>

using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int n;
    cin >> n;

    for (int i = 0; i < n; i++) {
        int g;
        cin >> g;
        int previous = -1;

        for (int j = 0; j < g; j++) {
            int current;
            cin >> current;

            if (previous >= 0 && previous + 1 != current) {
                cout << (1 + j) << "\n";
                string s;
                getline(cin, s);
                break;
            }

            previous = current;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM