-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3_gift1.cpp
124 lines (93 loc) · 2.64 KB
/
3_gift1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
ID: 27ckuzm1
TASK: gift1
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <sstream>
#include <cmath>
using namespace std;
int main() {
string line;
vector<string> array;
map<string, int> peopleDict;
vector<string> insertOrder;
ifstream inputFile("gift1.in");
string stringPeople;
getline (inputFile, stringPeople);
int numPeople = stoi(stringPeople);
// cout << numPeople << endl;
int i = 0;
string people[numPeople];
while (i < numPeople) {
string person;
getline (inputFile, person);
// cout << person << endl;
peopleDict.insert({person, 0});
insertOrder.push_back(person);
i++;
}
int indexer = 0;
while (indexer < numPeople) {
// get gift giver
string giftGiver;
getline (inputFile, giftGiver);
// cout << giftGiver << endl;
// get line of nums and process it into variables
string lineUnprocesssed;
getline (inputFile, lineUnprocesssed);
string token;
stringstream ss(lineUnprocesssed);
vector<string> num;
while (getline(ss, token, ' ')) {
num.push_back(token);
}
int amount = stoi(num[0]);
int numOfRecievers = stoi(num[1]);
// transform variables into needed ones
int BACK;
int APP;
if (numOfRecievers == 0) {
BACK = 0;
APP = 0;
} else {
BACK = amount % numOfRecievers;
APP = ceil(amount / numOfRecievers);
}
peopleDict[ giftGiver ] -= amount;
peopleDict[ giftGiver ] += BACK;
// cout << APP;
i = 0;
// grab gift recievers
while (i < numOfRecievers) {
string person;
getline (inputFile, person);
// cout << person << endl;
// cout << person;
peopleDict[ person ] += APP;
i++;
}
indexer++;
// cout << amount << giftGiver << recievers << endl << APP << endl;
}
ofstream outputFile;
outputFile.open("gift1.out");
for (int i = 0; i < insertOrder.size(); ++i) {
const string &s = insertOrder[i];
outputFile << s << ' ' << peopleDict[s] << '\n';
}
// for (auto itr = peopleDict.begin(); itr != peopleDict.end(); ++itr) {
// outputFile << itr->first << " " << itr->second << endl;
// }
while (getline (inputFile, line)) {
array.push_back(line);
}
inputFile.close();
outputFile.close();
return 0;
}