求助,为什么只有一个用例正确,自己测试的感觉没问题
查看原帖
求助,为什么只有一个用例正确,自己测试的感觉没问题
1229652
Annihilation19楼主2024/2/28 09:58
#include <stdio.h>
#include <stdlib.h>

struct Node
{
    int weight;
    int value;
    double w;
} coin[110];

int cmp(const void *a, const void *b)
{
    struct Node *coin_a = (struct Node *)a;
    struct Node *coin_b = (struct Node *)b;
    return coin_b->w - coin_a->w;
}

int main()
{
    int N, T;
    double s = 0;
    scanf("%d%d", &N, &T);
    for (int i = 0; i < N; i++)
    {
        scanf("%d%d", &coin[i].weight, &coin[i].value);
        coin[i].w = (double)coin[i].value / coin[i].weight; // 计算性价比
    }
    qsort(coin, N, sizeof(coin[0]), cmp); // 快速排序
    for (int i = 0; i < N; i++)
    {
        if (T >= coin[i].weight)
        {
            T -= coin[i].weight;
            s += coin[i].value;
        }
        else
        {
            s += coin[i].w * T;
            break;
        }
    }
    printf("%.2f", s);
    return 0;
}
2024/2/28 09:58
加载中...