# HDOJ 1009

0

View Code
```//hdoj 1009 豚鼠交易
#include <iostream>
#include <stdlib.h>
#include <iomanip>
using namespace std;
int m,n,i,j;
float sig=10000;
int flag;
float rest,smax=0;
int main(void){
while(cin>>m>>n){
if(m==-1&&n==-1) break;/////////
smax=0;
float *J=new float[n];
float *F=new float[n];
float *S=new float[n];
int *TAB=new int[n];
for(i=0;i<n;i++){
cin>>J[i]>>F[i];
S[i]=J[i]/F[i];
}
flag=0;
sig=0;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(S[j]!=-1){
if(sig<S[j]){
sig=S[j];
flag=j;
}
}
}
S[flag]=-1;
TAB[i]=flag;
sig=0;
}
rest=m;
for(i=0;i<n;i++){
if(rest>=F[TAB[i]]){
smax=smax+J[TAB[i]];
rest=rest-F[TAB[i]];
}else{
smax=smax+rest*(J[TAB[i]]/F[TAB[i]]);
rest=0;
}
}
cout<<setiosflags(ios::fixed)<<setprecision(3)<<smax<<endl;
smax=0;
}
return 0;
}```

coderLuan | 初学一级 | 园豆：197

0

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <iomanip>

using namespace
std;

struct
t_f
{

int
t,f;
};

int
n,m;
int
i;
double
sum;

int
cmp(t_f a,t_f b)
{

return
(double)a.t/a.f > (double)b.t/b.f;
}

int
main()
{

while
(cin>>m>>n&&(n!=-1||m!=-1))
{

vector<t_f>a(n);
for
(i=0;i<n;i++)
{

cin>>a[i].t>>a[i].f;
}

sort(a.begin(),a.end(),cmp);
sum=0;
for
(i=0;m&&i<n;i++)
{

if
(m>=a[i].f)
{

sum+=a[i].t;m-=a[i].f;
}

else

{

sum+=(double)(a[i].t)*m/a[i].f;m=0;
}
}

cout<<fixed<<setprecision(3)<<sum<<endl;
}

return
0;
}

VincentPass | 菜鸟二级 |园豆：238 | 2012-07-13 11:26

您需要登录以后才能回答，未注册用户请先注册