Pagini recente » Cod sursa (job #935109) | Cod sursa (job #1061584) | Cod sursa (job #2525978) | Cod sursa (job #870715) | Cod sursa (job #1060706)
#include <fstream>
using namespace std;
int main(void)
{
int G; // number of generators
int W; // quantity of energy necessary to start the central
int EG[1000]; // quantity of energy produced by a generator
int CG[1000]; // necessary cost for the production of energy
double R[1000]; // an array containing the price for producing one unit of energy
int sum; // the final cost
// Read data from the input file
ifstream fin ("energii.in");
fin >> G;
fin >> W;
for(int i = 0; i < G; i++)
{
fin >> EG[i];
fin >> CG[i];
}
fin.close();
for(int i = 0; i < G; i++)
R[i] = (1.0 * CG[i]) / EG[i];
// sort the R array in the increasing order. At the same time sort the arrays CG and EG.
for(int i = 0; i < G - 1; i++)
for(int j = i + 1; j < G; j++)
{
if(R[i] > R[j])
{
double a = R[i];
R[i] = R[j];
R[j] = a;
int b = EG[i];
EG[i] = EG[j];
EG[j] = b;
b = CG[i];
CG[i] = CG[j];
CG[j] = b;
}
}
sum = 0;
int numberOfGenerators = 0;
while (numberOfGenerators < G)
{
if(sum < W)
sum += CG[numberOfGenerators];
numberOfGenerators++;
}
if(sum < W)
sum = -1;
FILE *fout;
fout = fopen("energii.out", "w");
fprintf(fout, "%d\n", sum);
fclose(fout);
return 0;
}