本作品采用知识共享署名-相同方式共享 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;
}
}
}