Quick actions

cmd+k|ctrl+k

Navigation

Languages

Grand Theft Bank

Snippet info

Language

Cpp

Visibility

public

Author

mrsimb

Created

2017-03-05T08:31:52Z

Updated

2017-05-31T11:21:11Z

#include <stdio.h>
#include <stdlib.h>

// Объявление
struct Item;
void sortItems(struct Item* items, int itemsCount);
void printItems(struct Item* items, int itemsCount);
void printItem(struct Item item);
void robTheBank(struct Item* items, int itemsCount, float bagMaxWeight, float totalMaxWeight, int walksCount);

// Реализация
struct Item {
  char name[30];
  float weight;
  float value;
  int count;
};

void sortItems(struct Item* items, int itemsCount) {
  struct Item temporaryItem;
  float valuePerKg;

  for (int i = 0; i < itemsCount; i++) {
    valuePerKg = items[i].value / items[i].weight;

    for (int j = i + 1; j < itemsCount; j++) {
      if (items[j].value / items[j].weight > valuePerKg) {
        temporaryItem = items[j];
        items[j] = items[i];
        items[i] = temporaryItem;
      }
    }
  }
}

void printItems(struct Item* items, int itemsCount) {
  for (int i = 0; i < itemsCount; i++) {
    printItem(items[i]);
  }
}

void printItem(struct Item item) {
  printf("%s: %.2fkg %.2f$ (%d left)\n", item.name, item.weight, item.value, item.count);
}

void robTheBank(struct Item* items, int itemsCount, float bagMaxWeight, float totalMaxWeight, int walksCount) {
  float bagWeight = 0.0;
  float totalWeight = 0.0;

  float bagWeightAfter;
  float totalWeightAfter;

  for (int walk = 0; walk < walksCount; walk++) {
    for (int i = 0; i < itemsCount; i++) {
      for (int j = 0; items[i].count > 0; j++) {
        bagWeightAfter = bagWeight + items[i].weight;
        totalWeightAfter = totalWeight + bagWeightAfter;

        if (bagWeightAfter <= bagMaxWeight && totalWeightAfter <= totalMaxWeight) {
          bagWeight += items[i].weight;
          items[i].count--;

          printf("Item picked: ");
          printItem(items[i]);
        } else {
          break;
        }
      }
    }

    if (bagWeight == 0.0) {
      printf("Nothing to do else, imma gettin my ass out of there.\n");
      break;
    } else {
      printf("Bag packed, %.2fkg\n\n", bagWeight);
      totalWeight += bagWeight;
      bagWeight = 0.0;
    }
  }
  printf("Bank robbed, %.2fkg total.\n", totalWeight);
}

int main() {
// Исходные данные

/*
3
Platinum 1.2 180.0 5
Gold 1.0 230.0 4
Silver 0.6 34.0 10
15.0 5.0 10

*/

  int itemsCount;
  printf("Type items count:\n");
  scanf("%d", &itemsCount);

  struct Item items[itemsCount];

  printf("Type items attributes, dividing each attribute by space\n");
  printf("Name(string) Weight(float) Value(float) Count(int)\n");

  for (int i = 0; i < itemsCount; i++) {
    struct Item item;

    char name[30];
    float weight, value;
    int count;

    scanf("%s %f %f %d", &name, &weight, &value, &count);

    for (int j = 0; j < 30; j++) {
      item.name[j] = name[j];
    }

    item.weight = weight;
    item.value = value;
    item.count = count;

    items[i] = item;
  }

  float totalMaxWeight = 15.0;
  float bagMaxWeight = 5.0;
  int walksCount = 5;

  printf("Type total max weight, one bag weight and walks count:\n");
  printf("TotalMaxWeight(float) BagMaxWeight(float) WalksCount(int)\n");
  scanf("%f %f %d", &totalMaxWeight, &bagMaxWeight, &walksCount);

  printf("\nBank contains:\n");
  sortItems(items, itemsCount);
  printItems(items, itemsCount);
  printf("\n");

  robTheBank(items, itemsCount, bagMaxWeight, totalMaxWeight, walksCount);

  return 0;
}
INFO