PAT (Advanced Level) 1070. Mooncake (25)
简单贪心。先买性价比高的。
#include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<algorithm> using namespace std; double eps=1e-7; const int maxn=1000+10; int n; struct X { double x; double y; }s[maxn]; double money; bool cmp(const X&a,const X&b) { return a.y/a.x>b.y/b.x; } int main() { scanf("%d",&n); scanf("%lf",&money); for(int i=1;i<=n;i++) scanf("%lf",&s[i].x); for(int i=1;i<=n;i++) scanf("%lf",&s[i].y); sort(s+1,s+1+n,cmp); double ans=0; for(int i=1;i<=n;i++) { if(money>=s[i].x) { ans=ans+s[i].y; money=money-s[i].x; } else { ans=ans+money*s[i].y/s[i].x; break; } } printf("%.2lf\n",ans); return 0; }