4.买糖果
时间: 1ms 内存:128M
描述:
果店开业了,老板收到若干订单,贪心的老板喜欢先将订单总价值多的客户先发货,毕竟以盈利为主嘛,现在老板收到n份订单(n<20)以及这n份订单的详细信息,请按照订单总价值从大到小排序,并输出客户昵称。(后台数据保证没有总价值相同的订单)
输入:
分n+1行,第一行输入正整数n表示订单总数,剩下n行输入订单信息,第一个数字代表糖果单价(浮点数),第二个数字代表购货总数(正整数),第三个字符串代表客户昵称
输出:
按照订单总价由大到小分n行输出客户昵称
示例输入:
2
1 100 Mike
1.2 60 Joy
示例输出:
Mike
Joy
提示:
参考答案(内存最优[1092]):
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
const int maxn = 20;
struct node
{
double value;
int num;
char name[30];
} a[maxn];
int n;
int cmp(const void *x,const void *y)
{
node *xx = (node*) x;
node *yy = (node*) y;
return xx->value * xx->num < yy->value * yy->num;
}
int main()
{
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%lf%d%s",&a[i].value,&a[i].num,a[i].name);
qsort(a,n,sizeof(a[0]),cmp);
for(int i=0; i<n; i++)
puts(a[i].name);
return 0;
}
参考答案(时间最优[0]):
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
const int maxn = 20;
struct node
{
double value;
int num;
char name[30];
} a[maxn];
int n;
int cmp(const void *x,const void *y)
{
node *xx = (node*) x;
node *yy = (node*) y;
return xx->value * xx->num < yy->value * yy->num;
}
int main()
{
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%lf%d%s",&a[i].value,&a[i].num,a[i].name);
qsort(a,n,sizeof(a[0]),cmp);
for(int i=0; i<n; i++)
puts(a[i].name);
return 0;
}
题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。