您的位置:首页 > 其它

pat1020:月饼

2016-03-08 13:08 281 查看
https://www.patest.cn/contests/pat-b-practise/1020

#include "stdio.h"

struct yb{
float stock;
float price;
}yb[1000];
void swap(struct yb *a, struct yb *b)
{
struct yb temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int i, j, n, d;
float sum = 0;
scanf("%d %d", &n, &d);
for(i = 0; i < n; ++i)
{
scanf("%f", &yb[i].stock);
scanf("%f", &yb[i].price);
}
for(i = 0; i < n; ++i)
for(j = i+1; j < n; ++j)
if(yb[i].price / yb[i].stock < yb[j].price / yb[j].stock)
swap(&yb[i], &yb[j]);

for(i = 0; i < n; ++i)
if(yb[i].stock < d)
{
d -= (int)yb[i].stock;
sum += yb[i].price;
}
else
{
sum = sum + (float)(yb[i].price * (1.0 * d)/yb[i].stock);
break;
}
printf("%.2f\n", sum);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  pat C