求助,不清楚,这题哪里错了,自己运行的时候好像答案跟他是一样的。
#include <stdio.h>
#include <stdlib.h>
#include <string.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(); // 读取换行符
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;
}
可是它就是提示最小 n,答案错误