结构体的排序,用sort函数 或qsort。要细心,要缜密。 自己用这两个函数对结构体排序的时候总是把握不好,总是出错。。。多练习 多练习,多总结。
|
MilkTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 13686 Accepted Submission(s): 3344
Problem 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
sort排序代码:
#include<stdio.h>
#include<stdlib.h> struct s{ char s[1000]; int v,p; double dj; }b[1000]; int cmp(const void *a,const void *b) { if((*(struct s *)a).dj>(*(struct s *)b).dj) return 1; else if((*(struct s *)a).dj<(*(struct s *)b).dj) return -1; else return (*(struct s *)b).v-(*(struct s *)a).v; } int main() { int t; scanf("%d",&t); while(t--) { int n,i; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%s %d%d",b[i].s,&b[i].p,&b[i].v); getchar(); if(b[i].v>1200) b[i].v=1200; b[i].dj=(b[i].p*1.0)/b[i].v; } qsort(b,n,sizeof(b[0]),cmp); for(i=0;i<n;i++) { if(b[i].v<200) continue; else { printf("%s\n",b[i].s); break; } } } }
qsort 排序代码:
#include<stdio.h>
#include<cstring> #include<algorithm> using namespace std;
struct milk
{ char name[110]; // int pyuan; float price; int vkg; }po[110];
bool cmp(milk a,milk b)
{ if(a.price!=b.price) return a.price<b.price; else return a.vkg>b.vkg; } //char c[110]; int main() { int T,N,i,k,a,kg; float yuan,b; char str[110]; scanf("%d",&T); while(T--) { scanf("%d",&N); for(i=0,k=0;i<N;i++) { scanf("%s",str); scanf("%f %d",&yuan,&kg); if(kg>=200) { strcpy(po[k].name,str); a=kg/200; if(a>5) a=5; b=yuan/a; po[k].price=b; po[k].vkg=kg; k++; } } sort(po,po+k,cmp); printf("%s\n",po[0].name); } return 0; } |