PAT (Advanced Level) Practise 1070
来源:互联网 发布:软件购买 编辑:程序博客网 时间:2024/05/21 10:34
来源:http://pat.zju.edu.cn/contests/pat-a-practise/1070
同 http://pat.zju.edu.cn/contests/pat-b-practise/1020 相同,后者是中文。
算法应该比较简单,贪心。用单价从高到低排序就好。
要注意的是两个问题:
1. 市场需求数大于所有月饼总库存之和。(这样的话只要简单的把所有库存的月饼的总价加起来就OK);
2. 库存量和总售价原题目指的是 positive, 而不是 positive intergers,跟之前的不一样,所以用float类型的变量存起来。这个trick至少让笔者在三四个月时间里有至少十次都是同一个case没通过。
郁闷的无以复加。坑,就是坑。
#include<cstdio>#include<algorithm>using namespace std;typedef struct _tagMooncake{ float n, d, ratio;}Mooncake;bool cmp(const Mooncake& lhs, const Mooncake& rhs){ return lhs.ratio >= rhs.ratio;}Mooncake arr[1001];int main(){ int N, D; float cnt = 0.0, sum = 0.0; scanf("%d %d", &N, &D); for(int i=0; i<N; ++i) { scanf("%f", &arr[i].n); cnt += arr[i].n; } if (float(D) >= cnt) { cnt = 0; for(int i=0; i<N; ++i) { scanf("%f", &arr[i].d); cnt += arr[i].d; } printf("%.2f\n", float(cnt)); return 0; } for(int i=0; i<N; ++i) { scanf("%f", &arr[i].d); arr[i].ratio = float(arr[i].d) / arr[i].n; } sort(arr, arr+N, cmp); int i = 0; for(; i<N && D-arr[i].n>=0; ++i) { sum += arr[i].d; D -= arr[i].n; } if(i < N && D > 0) { sum += D * arr[i].ratio; } printf("%.2f\n", sum); return 0;}
0 0
- PAT (Advanced Level) Practise 1070
- [PAT] (Advanced Level) Practise
- PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1070 Mooncake (25)
- PAT (Advanced Level) Practise 1070 Mooncake (25)
- PAT (Advanced Level) Practise 1005
- pat (advanced level ) Practise 1065
- PAT (Advanced Level) Practise 1035
- PAT (Advanced Level) Practise 1031
- PAT (Advanced Level) Practise 1015
- 原生javascript-无间缝滚动,封装
- 鼠标多屏控制
- memcached源码分析之线程池机制(二)
- I帧、P帧和B帧的特点
- Oracle 方案
- PAT (Advanced Level) Practise 1070
- 值得关注的WordPress设计主题
- CSS清除浮动_清除float浮动
- 软件设计是怎样炼成的(4)——软件设计的“大道理”
- Memcached源码剖析系列之内存存储机制(一)
- php高性能、大数据、高并发、架构求答案、求资料
- Android 获取屏幕尺寸与密度
- PAT1005
- Linux C一站式学习习题答案4.1