-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tarjetas_Credito.cpp
333 lines (267 loc) · 7.07 KB
/
Tarjetas_Credito.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* bst.h
*
* Implementación del TAD arbol binario de búsqueda sin repeticiones con nodos enlazados usando shared_ptr
*
* Estructuras de Datos y Algoritmos
* Facultad de Informática
* Universidad Complutense de Madrid
*
* Copyright (c) 2017 Enrique Martín Martín. All rights reserved.
*/
#ifndef BST_EDA_H_
#define BST_EDA_H_
#include <memory> // shared_ptr, make_shared
#include <iomanip> // setw en graphAux
#include <iostream> // endl
using namespace std;
/*
T: tipo de los elementos almacenados en el bst
Compare: clase que implementa operator() aceptando 2 referencias constantes a valores de tipo T, y devuelve
si el primer valor es "menor" que el segundo. Debe ser un 'strict weak ordering':
(1) irreflexivo, (2) asimétrico, (3) transitivo y con (4) transitividad de la incomparabilidad.
(Ver más detalles en https://en.wikipedia.org/wiki/Weak_ordering#Strict_weak_orderings)
*/
template <typename T, typename Compare = less<T>>
class bst {
private:
const int TREE_INDENTATION = 4;
/*
Nodo que almacena internamente el elemento (de tipo T)
y dos 'punteros unicos', uno al hijo izquierdo y al hijo derecho.
*/
class Nodo; // Declaración adelantada para poder definir Link
using Link = shared_ptr<Nodo>; // Alias de tipo
class Nodo {
public:
Link izq;
T elem;
Link der;
Nodo(const T& elem) : izq(nullptr), elem(elem), der(nullptr) {}
Nodo(Link izq, const T& elem, Link der) : izq(izq), elem(elem), der(der) {}
};
// puntero a la raíz
Link raiz;
// objeto comparador
Compare cmp;
// busqueda de nodo
pair<bool, T> search_rec(const T& e, const Link& raiz) const {
if (raiz == nullptr) {
return make_pair(false, e);
}
else if (cmp(e, raiz->elem)) {
return search_rec(e, raiz->izq);
}
else if (cmp(raiz->elem, e)) {
return search_rec(e, raiz->der);
}
else { // e == nodo->elem
return make_pair(true, raiz->elem);
}
}
// cálculo recursivo del tamaño
size_t size_rec(const Link& nodo) const {
if (nodo == nullptr) {
return 0;
}
else {
return size_rec(nodo->izq) + size_rec(nodo->der) + 1;
}
}
// inserción recursiva de elemento
void insert_rec(Link& raiz, const T& e) {
if (raiz == nullptr) {
raiz = make_shared<Nodo>(e);
}
else if (cmp(e, raiz->elem)) {
insert_rec(raiz->izq, e);
}
else if (cmp(raiz->elem, e)) {
insert_rec(raiz->der, e);
}
else {
// Si e == raiz-elem el elemento ya esta, puedo lanzar una excepcion
// o no hacer nada
}
}
// Dada la raiz de un árbol NO VACIO (es decir, raiz != nullptr), borra el nodo mínimo y
// devuelve el elemento que había ahí
void remove_min(Link& raiz, T& min) {
if (raiz->izq == nullptr) { // El nodo raiz contiene el mínimo
min = raiz->elem;
raiz = raiz->der; // El anterior nodo al que apuntaba raiz ya no tiene dueño, se libera la memoria
}
else {
remove_min(raiz->izq, min);
}
}
void remove_rec(Link& raiz, const T& e) {
if (raiz == nullptr) // Imposible eliminar en árbol vacío
return;
if (cmp(e, raiz->elem)) {
remove_rec(raiz->izq, e);
}
else if (cmp(raiz->elem, e)) {
remove_rec(raiz->der, e);
}
else if (raiz->der == nullptr) { // e == raiz->elem
raiz = raiz->izq;
}
else { // e == raiz->elem && raiz->der != nullptr
T min;
remove_min(raiz->der, min);
raiz->elem = min;
}
}
// Muestra por 'out' una representación del árbol
// Adaptado de "ADTs, DataStructures, and Problem Solving with C++", Larry Nyhoff, Person, 2015
void graph_rec(ostream & out, int indent, Link raiz) const {
if (raiz != nullptr) {
graph_rec(out, indent + TREE_INDENTATION, raiz->der);
out << setw(indent) << " " << raiz->elem << endl;
graph_rec(out, indent + TREE_INDENTATION, raiz->izq);
}
}
// Realiza una copia de todos los nodos que cuelgan de 'raiz'
Link copia(Link raiz) {
if (raiz == nullptr) {
return nullptr;
}
else {
Link ni = copia(raiz->izq);
Link nd = copia(raiz->der);
return make_shared<Nodo>(ni, raiz->elem, nd);
}
}
public:
// constructor de bst vacío
bst() : raiz(nullptr) {}
// constructor por copia
bst(const bst<T, Compare>& other) {
raiz = copia(other.raiz);
}
// Asignacion
bst<T, Compare>& operator=(const bst<T, Compare>& other) {
if (this != &other) {
raiz.reset();
raiz = copia(other.raiz);
}
return *this;
}
// saber si el bst es vacío
bool empty() const {
return (raiz == nullptr);
}
// tamaño del bst
size_t size() const {
return size_rec(raiz);
}
// buscar un elemento
pair<bool, T> search(const T& e) const {
return search_rec(e, raiz);
}
// insertar un elemento
void insert(const T& e) {
insert_rec(raiz, e);
}
// borrar un elemento
void remove(const T& e) {
remove_rec(raiz, e);
}
// Muestra por 'out' una representación del árbol
// Adaptado de "ADTs, DataStructures, and Problem Solving with C++", Larry Nyhoff, Person, 2015
void graph(ostream & out) const {
out << "==== Tree =====" << endl;
graph_rec(out, 0, raiz);
out << "===============" << endl;
}
};
#endif /* BST_EDA_H_ */
//CLASE MAP
template<typename Key, typename Value, typename Compare = less<Key>>
class map {
private:
//Hay que redefinir el elemento comparador
class comparePair{
public:
Compare cmp;
//Me devuelve true si Key de A es menor que Key de b
bool operator() (const pair<Key, Value>& a, const pair<Key, Value>& b) const {
return cmp(a.first, b.first);
}
};
bst<pair<Key, Value>, comparePair> bst_;
public:
//Crea el bst vacío
map(){
bst_ = bst<pair<Key, Value>, comparePair>();
}
//Devolvemos el valor asociado a la clave key
Value get(const Key& key) const {
pair<Key, Value> p;
p.first = key;
return bst_.search(p).second.second;
}
//Comprobamos si key aparece en el diccionario
bool contains(const Key& key) const {
pair<Key, Value> p;
p.first = key;
return bst_.search(p).first;
}
//Insertamos el valor 'value' asociado a la clave 'key' en el diccionario.
//Si el diccionario ya contiene dicha clave, se actualiza el valor asociado
void insert(const Key& key, const Value& value) {
pair<Key, Value> p;
p = make_pair(key, value);
if (contains(p.first))
erase(p.first);
bst_.insert(p);
}
//Eliminamos la clave 'key' del diccionario
void erase(const Key& key) {
pair<Key, Value> p;
p.first = key;
if (contains(p.first))
bst_.remove(p);
}
};
//PROGRAMA PRINCIPAL
int main()
{
int numCasos;
cin >> numCasos;
map<int, double> map_;
for(int i = 0; i < numCasos; i++)
{
int numTarjetas;
cin >> numTarjetas;
map_ = map<int, double>();
for (int j = 0; j < numTarjetas; j++)
{
int tarjeta;
double dinero;
cin >> tarjeta;
cin >> dinero;
if (!map_.contains(tarjeta))
map_.insert(tarjeta, dinero);
else {
double dineroTarjeta;
dineroTarjeta = map_.get(tarjeta);
double op = dineroTarjeta + dinero;
map_.insert(tarjeta, op);
}
}
int numConsultas;
cin >> numConsultas;
for (int k = 0; k < numConsultas; k++)
{
int tarjeta;
cin >> tarjeta;
if (map_.contains(tarjeta))
cout << tarjeta << " " << setprecision(2) << fixed << map_.get(tarjeta) << endl;
else
cout << tarjeta << " " << "0.00" << endl;
}
}
return 0;
}