Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <vector>
- #include <algorithm>
- using namespace std;
- vector<string> vectorFiller(string s, char sep) {
- vector<string> v;
- string word = "";
- for (int i = 0; i < s.length(); i++) {
- if (s[i] == sep) {
- v.push_back(word);
- word = "";
- }
- else {
- word += s[i];
- }
- }
- v.push_back(word);
- return v;
- }
- int indexFinder(string s, vector<string> v) {
- for (int i = 0; i < v.size(); i++) {
- if (s == v[i]) {
- return i;
- }
- }
- return -1;
- }
- vector<string> vectorCutter(vector<string> v, int index) {
- vector<string> sub;
- for (int i = index; i <= v.size(); i++) {
- sub.push_back(v[index]);
- v.erase(v.begin() + index);
- }
- return sub;
- }
- int main() {
- string line;
- getline(cin, line);
- auto chest = vectorFiller(line, '|');
- while (true) {
- getline(cin, line);
- auto tokens = vectorFiller(line, ' ');
- string command = tokens[0];
- if (command == "Yohoho!") {
- break;
- }
- else if (command == "Loot") {
- for (int i = 1; i < tokens.size(); i++) {
- if (indexFinder(tokens[i], chest) == -1) {
- chest.insert(chest.begin(), tokens[i]);
- }
- }
- }
- else if (command == "Drop") {
- int index = stoi(tokens[1]);
- if (index >= 0 && index < chest.size()) {
- chest.push_back(chest[index]);
- chest.erase(chest.begin() + index);
- }
- }
- else if (command == "Steal") {
- int count = stoi(tokens[1]);
- for (int i = chest.size() - count; i < chest.size() - 1; i++) {
- cout << chest[i] << ", ";
- }
- cout << chest.back() << endl;
- chest.resize(chest.size() - count);
- }
- }
- if (chest.size() == 0) {
- cout << "Failed treasure hunt.\n";
- }
- else {
- double chestItemLength = 0;
- for (int i = 0; i < chest.size(); i++) {
- chestItemLength += chest[i].length();
- }
- cout << "Average treasure gain: " << fixed << setprecision(2) << chestItemLength / chest.size() << " pirate credits.\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement