170411 VJ题解(2)(ZOJ - 3875)

本文介绍了一道关于结构体排序的应用题,通过C语言实现,详细解释了如何选取特定菜品并计算总价的过程。适用于初学者理解结构体及排序算法的实际运用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

知识共享许可协议
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。

题目地址: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3875

思路

本题考察结构体的应用。将所输入的菜名及其价格按价格从低到高排列,利用C语言中整数除法向下取整的特性,取每种菜的第 N/2+1 道菜,最后输出三道菜的总价及各自的名称。

需要注意的地方

1.取 N/2+1 的前提是存储菜的数据时从 “1” 而不是从 “0” 开始。
2.本题数据量小,排序方式随意。

Show me the code

#include <bits/stdc++.h>

using namespace std;


typedef struct node
{
    char name[505];
    int price;
} snode;

bool cmp(snode x, snode y)
{
    return x.price < y.price;
}

int main()
{
    int m, n, t, s, d, c;
    snode a[1001];
    while(cin >> t)
    {
        while(t--)
        {
            memset(a, 0, sizeof(a));
            snode b[3];
            cin >> s >> m >> d;
            for(int i = 1; i <= s; i++)
            {
                cin >> a[i].name >> a[i].price;
            }
            sort(a + 1, a + s + 1, cmp);
            b[0] = a[s / 2 + 1];
            c = b[0].price;
            for(int i = 1; i <= m; i++)
            {
                cin >> a[i].name >> a[i].price;
            }
            sort(a + 1, a + m + 1, cmp);
            b[1] = a[m / 2 + 1];
            c += b[1].price;
            for(int i = 1; i <= d; i++)
            {
                cin >> a[i].name >> a[i].price;
            }
            sort(a + 1, a + d + 1, cmp);
            b[2] = a[d / 2 + 1];
            c += b[2].price;
            cout << c << " " << b[0].name << " " << b[1].name << " " << b[2].name << endl;

        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值