Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <iostream>
- #include <algorithm>
- #include <fstream>
- std::ifstream FileIn("Task.txt");
- void bubbleSortForAlg(double** arr, int n) {
- double temp = 0;
- for (size_t i = 0; i < n-1; i++) {
- for (size_t j = 0; j < n - i - 1; ++j)
- if (arr[j][2] > arr[j+1][2]) {
- for (size_t k = 0; k < 3; ++k) {
- temp = arr[j][k];
- arr[j][k] = arr[j+1][k];
- arr[j+1][k] = temp;
- }
- }
- }
- std::cout << std::endl;
- }
- void printArr(double** array, int numberOfItem) {
- for (size_t i = 0; i < numberOfItem; ++i) {
- for (size_t j = 0; j < 3; ++j) {
- std::cout << array[i][j] << "\t";
- }
- std::cout << std::endl;
- }
- }
- double greedyAlg(double *arrayOfCost, double *arrayOfWeight, int maxWeight, int numberOfItem) {
- double** arrayOfWeightAndUnitValue = new double* [numberOfItem];
- for (size_t i = 0; i < numberOfItem; ++i) {
- arrayOfWeightAndUnitValue[i] = new double[3];
- }
- for (size_t i = 0; i < numberOfItem; ++i) {
- arrayOfWeightAndUnitValue[i][0] = arrayOfWeight[i];
- arrayOfWeightAndUnitValue[i][1] = arrayOfCost[i];
- arrayOfWeightAndUnitValue[i][2] = arrayOfWeightAndUnitValue[i][1]/arrayOfWeightAndUnitValue[i][0];
- }
- //printArr(arrayOfWeightAndUnitValue, numberOfItem);
- bubbleSortForAlg(arrayOfWeightAndUnitValue, numberOfItem);
- //printArr(arrayOfWeightAndUnitValue, numberOfItem);
- double curentWeight = 0, maxCost = 0;
- int count = 0;
- for (int i = (numberOfItem-1); i >=0; --i) {
- if (arrayOfWeightAndUnitValue[i][0] + curentWeight <= maxWeight) {
- curentWeight += arrayOfWeightAndUnitValue[i][0];
- maxCost += arrayOfWeightAndUnitValue[i][1];
- count++;
- }
- else {
- if (curentWeight + arrayOfWeightAndUnitValue[i][0] > maxWeight && curentWeight < maxWeight) {
- maxCost += arrayOfWeightAndUnitValue[i][2] * (maxWeight - curentWeight);
- curentWeight += arrayOfWeightAndUnitValue[i][0] * (maxWeight - curentWeight);
- }
- count++;
- }
- }
- std::cout << "It is necessary to pick item with characteristic \nweight cost\n";
- if (count < numberOfItem) {
- for (int i = numberOfItem - 1; i >= count; --i) {
- for (size_t j = 0; j < 2; ++j) {
- std::cout << arrayOfWeightAndUnitValue[i][j] << "\t";
- }
- std::cout<<std::endl;
- }
- }
- else {
- for (int i = numberOfItem - 1; i >= 0; --i) {
- for (size_t j = 0; j < 2; ++j) {
- std::cout << arrayOfWeightAndUnitValue[i][j] << "\t";
- }
- std::cout << std::endl;
- }
- }
- for (size_t i = 0; i < numberOfItem; ++i) {
- delete[] arrayOfWeightAndUnitValue[i];
- }
- delete[] arrayOfWeightAndUnitValue;
- return maxCost;
- }
- int main() {
- int maxWeight = 0, numberOfItem = 0;
- FileIn >> numberOfItem >> maxWeight;
- double* arrayOfWeight = new double[numberOfItem];
- double* arrayOfCost = new double[numberOfItem];
- for (size_t i = 0; i < numberOfItem; ++i) {
- FileIn >> arrayOfWeight[i];
- }
- for (size_t i = 0; i < numberOfItem; ++i) {
- FileIn >> arrayOfCost[i];
- }
- double resultOfGreedyAlg = greedyAlg(arrayOfCost, arrayOfWeight, maxWeight, numberOfItem);
- std::cout <<"result is " <<resultOfGreedyAlg;
- delete[] arrayOfWeight;
- delete[] arrayOfCost;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement