编辑代码

#include <stdio.h>
#include <stdlib.h>

struct SightseeingSpot {
    char name[20];
    int time;
    int score;
    float value;
};

int compare(const void *a, const void *b) {
    struct SightseeingSpot *spotA = (struct SightseeingSpot *)a;
    struct SightseeingSpot *spotB = (struct SightseeingSpot *)b;
    return (spotB->value - spotA->value) > 0 ? 1 : -1;
}

int main() {
    struct SightseeingSpot spots[] = {
        {"故宫", 1, 7, 0},
        {"颐和园", 2, 8, 0},
        {"长城", 3, 9, 0},
        {"天坛", 1, 6, 0}
    };

    int num_spots = sizeof(spots) / sizeof(spots[0]);

    for (int i = 0; i < num_spots; ++i) {
        spots[i].value = (float)spots[i].score / spots[i].time;
    }

    qsort(spots, num_spots, sizeof(spots[0]), compare);

    int total_time = 4;
    int current_time = 0;
    int total_score = 0;

    printf("选择的最优景点:\n");
    for (int i = 0; i < num_spots; ++i) {
        if (current_time + spots[i].time <= total_time) {
            printf("%s\n", spots[i].name);
            current_time += spots[i].time;
            total_score += spots[i].score;
        }
    }

    printf("评分总和:%d\n", total_score);

    return 0;
}