求助,不清楚这题哪里出错了,自己运行时看起来答案和预期一样,但还是提示最小值错误。
#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);
books[i].title[strcspn(books[i].title, "\n")] = '\0'; // 移除换行符
scanf("%f", &books[i].price);
}
if(n == 1) {
printf("%.2f, %s\n", 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\n", books[0].price, books[0].title);
printf("%.2f, %s\n", books[n - 1].price, books[n - 1].title);
return 0;
}