Milk(sort+结构体)

Description

Ignatius drinks milk everyday, now he is in the supermarket and he wants to choose a bottle of milk. There are many kinds of milk in the supermarket, so Ignatius wants to know which kind of milk is the cheapest.

Here are some rules: 
1. Ignatius will never drink the milk which is produced 6 days ago or earlier. That means if the milk is produced 2005-1-1, Ignatius will never drink this bottle after 2005-1-6(inclusive). 
2. Ignatius drinks 200mL milk everyday. 
3. If the milk left in the bottle is less than 200mL, Ignatius will throw it away. 
4. All the milk in the supermarket is just produced today.

Note that Ignatius only wants to buy one bottle of milk, so if the volumn of a bottle is smaller than 200mL, you should ignore it. 
Given some information of milk, your task is to tell Ignatius which milk is the cheapest.

Input

The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow. 
Each test case starts with a single integer N(1<=N<=100) which is the number of kinds of milk. Then N lines follow, each line contains a string S(the length will at most 100 characters) which indicate the brand of milk, then two integers for the brand: P(Yuan) which is the price of a bottle, V( mL) which is the volume of a bottle.

Output

For each test case, you should output the brand of the milk which is the cheapest. If there are more than one cheapest brand, you should output the one which has the largest volume.

Sample Input

2
2
Yili 10 500
Mengniu 20 1000
4
Yili 10 500
Mengniu 20 1000
Guangming 1 199
Yanpai 40 10000 

Sample Output

Mengniu
Mengniu 

Hint

In the first case, milk Yili can be drunk for 2 days, it costs 10 Yuan. Milk Mengniu can be drunk for 5 days, it costs 20 Yuan. So Mengniu is the cheapest.In the second case, milk Guangming should be ignored. Milk Yanpai can be drunk for 5 days, but it costs 40 Yuan. So Mengniu is the cheapest.
 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 struct nam
 5 {
 6     int v;
 7     float y;
 8     char a[500];
 9 } milk[200];
10 bool f(nam a,nam b)
11 {
12     double ka,kb;
13     int da,db;
14     da=(a.v/200)>5?5:(a.v/200);
15     db=(b.v/200)>5?5:(b.v/200);
16     ka=a.y*1.0/da;
17     kb=b.y*1.0/db;
18     if(ka!=kb) return ka<kb;
19     else
20     return a.v>b.v;
21
22
23 }
24 int main()
25 {
26     int t,n;
27     scanf("%d",&t);
28     while(t--)
29     {
30         scanf("%d",&n);
31         for(int i=0;i<n;i++)
32         {
33         scanf("%s %f %d",&milk[i].a,&milk[i].y,&milk[i].v);
34         if(milk[i].v<200)
35         {
36             i--;
37             n--;
38         }
39
40         }
41         sort(milk,milk+n,f);
42         printf("%s\n",milk[0].a);
43
44    }
45 }
时间: 2024-08-19 04:02:56

Milk(sort+结构体)的相关文章

sort+结构体实现二级排序

之前介绍的sort函数由于其效率较高,使用较为简单让我用起来那叫一个爽,今天再写一篇使用sort+结构体实现二级排序的方法. 还是先想个问题吧,比如我想输入5个同学的名字和身高,然后得到他们身高的降序,但是如果出现相同身高的情况,名字的拼音靠前的排在前面. 好,现在这个问题已经涉及到了二级排序,要按照身高的降序和姓名的升序排列,那么就要先定义一个结构体,将姓名和身高都包含进去,然后用sort对结构体排序,而实现二级排序,关键在于自己写的cmp函数(sort的比较方法) 1 #include<io

杭电 1862 EXCEL排序(sort+结构体)

Description Excel可以对一组纪录按任意指定列排序.现请你编写程序实现类似功能. Input 测试输入包含若干测试用例.每个测试用例的第1行包含两个整数 N (<=100000) 和 C,其中 N 是纪录的条数,C 是指定排序的列号.以下有 N 行,每行包含一条学生纪录.每条学生纪录由学号(6位数字,同组测试中没有重复的学号).姓名(不超过8位且不包含空格的字符串).成绩(闭区间[0, 100]内的整数)组成,每个项目间用1个空格隔开.当读到 N=0 时,全部输入结束,相应的结果不

【结构体排序】hdu 2409 Team Arrangement

[结构体排序]hdu 2409 Team Arrangement 题目链接:hdu 2409 Team Arrangement 题目大意 给出22个球员的各种信息,要求按照给出的阵形选择球员和队长共11人: 选择球员的规则是:同角色的球员按照编号从小到大选择,直到选够此角色的人数: 选择队长的规则是:在所有已经被选择为某角色的球员中,选择服役时间最长的那个球员, 如果服役时间时间相同,就选择编号较大的那一个球员: 2006 Asia Regional Tehran(伊朗首都德黑兰)的第一题,题意

【转】 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法

sort函数在使用中非常好用,也非常简单,而且效率与冒泡或者选择排序不是一个数量级.本文就sort函数在vector中的用法分为sort函数入门用法与自定义comp比较函数比较结构体这两个最基本的功能讲讲其用法: 1.sort入门: 使用sort需要包含algorithm头文件,完整代码如下 #include<iostream> #include<vector> #include<algorithm>//貌似可以不用,但最好加上. using namespace std

【STL学习】sort函数之自定义结构体数组

最近经常用到结构体数组排序,所以把用sort对结构体数组排序整理一下. #include<iostream> #include<algorithm>//需要加该头文件 using namespace std; struct define{ int a; int b; }d[10]; bool compare(const define &x,const define &y); int main() { for(int i=0;i<10;i++){ cin>

C++ 结构体多元素sort排序调用时的写法

//总结一下,结构体数据排序的快速写法 //以后在遇到需要写的时候,不要迟疑快速写完 struct node { int u, v, w; }a[10000]; //假设该结构体有3个元素 //现在仅实现结构体数组按照w的值从小到大的排序 //1.基于C++的重载写法,写在结构体的定义内 如下: struct node { int u, v, w; bool operator <(const node &x)const { return w<x.w; //升序排列 } }; //现在提

结构体快排回顾(sort)

一般来说,我做竞赛的时候排序一般用快排 很快很方便 普通sort(从小到大) sort(a,a+n); 直接贴一段代码吧,包含了vector,sort,结构体等简单东西综合 #include <iostream> #include <vector> #include <algorithm> using namespace std; typedef struct example { int elem1; int elem2; }example; /*这个compariso

转载 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法

转载自:http://www.cnblogs.com/cj695/p/3863142.html sort函数在使用中非常好用,也非常简单,而且效率与冒泡或者选择排序不是一个数量级.本文就sort函数在vector中的用法分为sort函数入门用法与自定义comp比较函数比较结构体这两个最基本的功能讲讲其用法: 1.sort入门: 使用sort需要包含algorithm头文件,完整代码如下 #include<iostream> #include<vector> #include<

HDU - 1789 Doing Homework again(贪心) ~~~学了一波sort对结构体排序

题目中因为天数和分数是对应的,所以我们使用一个结构体来存分数和截止如期. 一开始做这道题的时候,很自然的就想到对天数排序,然后天数一样的分数从大到小排序,最后WA了之后才发现没有做到"舍小取大"的贪心.所以改变一下策略,对分数排序,如果分数一样的话,时间从小到大排序(因为我们的目的就是先做分多的作业,所以分数一样的得放到前几天去做). (具体sort排结构体知识见代码里面,其实也可以写两次for来排序): 思路:排好序之后,从小到大遍历,每找到一个分数,去寻找对应的天数到第一天中有没有