Pagini recente » Cod sursa (job #1861425) | Cod sursa (job #1250308) | Cod sursa (job #2352825) | Cod sursa (job #564877) | Cod sursa (job #469999)
Cod sursa(job #469999)
#include<fstream>
using namespace std;
int g,w,energ[1002][2];
float rap[1002];
int main(){
ifstream f("energii.in");
ofstream o("energii.out");
f>>g>>w;
int i;
float a,b;
for(i=1;i<=g;i++){
f>>a>>b;
if(b==0){
rap[i]=9999;
}
else{
rap[i]=a/b;
}
energ[i][0]=(int)a;
energ[i][1]=(int)b;
}
short ok=0;
while(ok==0){
ok=1;
for(i=1;i<g;i++){
if(rap[i]<rap[i+1]){
float rez;
rez=rap[i];
rap[i]=rap[i+1];
rap[i+1]=rez;
rez=energ[i][0];
energ[i][0]=energ[i+1][0];
energ[i+1][0]=(int)rez;
rez=energ[i][1];
energ[i][1]=energ[i+1][1];
energ[i+1][1]=(int)rez;
ok=0;
}
}
}
ok=0;
while(ok==0){
ok=1;
for(i=1;i<g;i++){
if(rap[i]==rap[i+1] && energ[i][0]<energ[i+1][0]){
float rez;
rez=rap[i];
rap[i]=rap[i+1];
rap[i+1]=rez;
rez=energ[i][0];
energ[i][0]=energ[i+1][0];
energ[i+1][0]=(int)rez;
rez=energ[i][1];
energ[i][1]=energ[i+1][1];
energ[i+1][1]=(int)rez;
ok=0;
}
}
}
int cons=0;
i=1;
while(w>0){
w=w-energ[i][0];
cons+=energ[i][1];
i++;
if(i>g && w>0){
o<<"-1";
w=-5578;
}
}
if(w!=-5578);
{
o<<cons;
}
}