C language help

Help, I’m not sure where this problem is wrong; when I run it myself, the answer seems to be the same as theirs.

#include <cstdio.h>
#include <cstdlib.h>
#include <cstring.h>

struct book {
    char title[31];
    float price;
};

int main() {
    int n;
    scanf("%d", &n);
    if(n <= 0 || n >= 10) {
        return 0;
    }
    struct book books[n];
    for (int i = 0; i < n; i++) {
        getchar();  // read newline character
        fgets(books[i].title, sizeof(books[i].title), stdin);
        scanf("%f", &books[i].price);
    }
    if(n == 1) {
        printf("%.2f, %s", books[0].price, books[0].title);
        return 0;
    }

    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (books[j].price < books[j + 1].price) {
                struct book temp = books[j];
                books[j] = books[j + 1];
                books[j + 1] = temp;
            }
        }
    }

    printf("%.2f, %s", books[0].price, books[0].title);
    printf("%.2f, %s", books[n - 1].price, books[n - 1].title);

    return 0;
}

image
image

But it still prompts the minimum n, answer is wrong.

It is recommended to look at other people’s solutions, see how they handle the output, and notice any differences from your own method.

You can ask GPT first, and if GPT can’t handle it, then switch to a human :grin:

Consider the sample hint