From 6c957dc4e01aee6ce9cae3c8342d04b0fd9ca9c4 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Tue, 25 Apr 2017 17:54:10 +0200 Subject: Added new files from PRE OII 2017 --- bicicletta.c++ | 62 ++++++++++++++++++++++++++++++++ carte.cpp | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ parentesi.cpp | 58 ++++++++++++++++++++++++++++++ sorveglianza.c++ | 88 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 313 insertions(+) create mode 100644 bicicletta.c++ create mode 100644 carte.cpp create mode 100644 parentesi.cpp create mode 100644 sorveglianza.c++ diff --git a/bicicletta.c++ b/bicicletta.c++ new file mode 100644 index 0000000..bb42031 --- /dev/null +++ b/bicicletta.c++ @@ -0,0 +1,62 @@ +/* INPUT: +3 4 +2 +3 +1 +1 3 +1 2 +3 2 +3 1 + +OUTPUT: +3 +*/ + +#include +#include + +using namespace std; + +int main(void) +{ + ifstream in; + ofstream out; + in.open("input.txt"); + out.open("output.txt"); + + int numBici, sorpassi, i, x, j, y, m[2]; + in >> numBici; + in >> sorpassi; + + int* posBici = new int[numBici]; + + // posizioni di base + for(i = 0; i < numBici; i++) + in >> posBici[i]; + + // sorpassi + for(i = 0; i < sorpassi; i++) + { + in >> x; + in >> y; + + for(j = 0; j < numBici; j++) + { + if(posBici[j] == x) m[0] = j; //indice valore che sorpassa + + if(posBici[j] == y) m[1] = j; //indice valore che viene sorpassato + } + + posBici[m[0]] = y; + posBici[m[1]] = x; + } + + out << posBici[0]; + + delete[] posBici; + + in.close(); + out.close(); + + return 0; +} diff --git a/carte.cpp b/carte.cpp new file mode 100644 index 0000000..697622a --- /dev/null +++ b/carte.cpp @@ -0,0 +1,105 @@ +/* +INPUT: +18 +12 6 +11 8 +15 8 +15 20 +13 18 +12 23 +11 40 +15 19 +11 25 +13 36 +15 12 +12 18 +17 11 +23 9 +28 22 +35 26 +11 23 +15 12 + +output: +11 96 */ +#include +#include + +using namespace std; + +struct punteggioGiocatori +{ + int idGiocatore; + int punteggio; +} player[100]; + +int ricercaIdGiocatore(int valore, struct punteggioGiocatori* p); + +int main() +{ + ifstream in("input.txt"); + ofstream out("output.txt"); + + int N, i, pPlayer, j = 0, rID, nVal, t[2]; + bool s; + in >> N; + + for(i = 0; i < N; i++) { + player[i].idGiocatore = 0; + player[i].punteggio = 0; + } + + for(i = 0; i < N; i++) { + in >> pPlayer; + rID = ricercaIdGiocatore(pPlayer, player); + if(rID == -1) { + player[j].idGiocatore = pPlayer; + in >> player[j].punteggio; + j++; + } else { + in >> nVal; + player[rID].punteggio += nVal; + } + } + int last = j; + do { + s = false; + for(int k = 0; k < last; k++) + { + if(player[k].punteggio < player[k+1].punteggio) { + t[0] = player[k].idGiocatore; + t[1] = player[k].punteggio; + + player[k].idGiocatore = player[k+1].idGiocatore; + player[k].punteggio = player[k+1].punteggio; + + player[k+1].idGiocatore = t[0]; + player[k+1].punteggio = t[1]; + + s = true; + } + + } + + last--; + } while(s); + + out << player[0].idGiocatore << " " << player[0].punteggio << endl; + + in.close(); + out.close(); +} + +int ricercaIdGiocatore(int valore, struct punteggioGiocatori* p) +{ + int rID = -1; + for(int i = 0; i < 100; i++) + { + if(p[i].idGiocatore == valore) { + rID = i; + break; + } + } + + return rID; +} diff --git a/parentesi.cpp b/parentesi.cpp new file mode 100644 index 0000000..159b75f --- /dev/null +++ b/parentesi.cpp @@ -0,0 +1,58 @@ +/* INPUT: +[ ( ( [ { { [ ] ] ( ( ( } } ( ) ) ) ) ) ) ] ] ] + +output: +11 */ +#include +#include +#include + +using namespace std; + +int main() +{ + ifstream in; + ofstream out; + in.open("input.txt"); + out.open("output.txt"); + + int i; + int t = 0, q = 0, g = 0; //tonde, quadre, graffe + char str[1000], c; + i = 0; + while(!in.eof()) + { + in.get(c); + if(c == ' ') continue; + else { + str[i] = c; + i++; + } + } + + int n = strlen(str), tot = 0; + + for(int i = 0; i < n; i++) + { + if(str[i] == '(') t++; + else if(str[i] == '[') q++; + else if(str[i] == '{') g++; + + if(str[i] == ')' && t > 0) { + t--; + tot++; + } else if(str[i] == ']' && q > 0) { + q--; + tot++; + } else if(str[i] == '}' && g > 0) { + g--; + tot++; + } + } + + out << tot << endl; + + in.close(); + out.close(); + return 0; +} diff --git a/sorveglianza.c++ b/sorveglianza.c++ new file mode 100644 index 0000000..4154596 --- /dev/null +++ b/sorveglianza.c++ @@ -0,0 +1,88 @@ +/* INPUT: +10 +6 +2 5 +0 2 +1 3 +5 6 +4 7 +7 9 + +OUTPUT: +4*/ +#include +#include +#include + +using namespace std; + +int main(void) +{ + ifstream in; + ofstream out; + in.open("input.txt"); + out.open("output.txt"); + + int giorni, turniTot, i, j, tot = 0, m[2], x; + + in >> giorni; + in >> turniTot; + + vector< vector > turni(turniTot, vector(2)); + int* eDay = new int[giorni]; + + for(i = 0; i < giorni; i++) eDay[i] = 0; + + for(i = 0; i < turniTot; i++) + { + for(j = 0; j < 2; j++) + { + in >> x; + turni[i][j] = x; + } + } + + for(i = 0; i < turniTot-1; i++) + { + if(turni[i][0] > turni[i+1][0]) { + m[0] = turni[i][0]; + m[1] = turni[i][1]; + + turni[i][0] = turni[i+1][0]; + turni[i][1] = turni[i+1][1]; + + turni[i+1][0] = m[0]; + turni[i+1][1] = m[1]; + } + + if(turni[i][1] == giorni-1) { + m[0] = turni[i][0]; + m[1] = turni[i][1]; + + turni[i][0] = turni[i+1][0]; + turni[i][1] = turni[i+1][1]; + + turni[i+1][0] = m[0]; + turni[i+1][1] = m[1]; + } + } + + for(i = 0; i < turniTot-1; i++) + { + if(eDay[turni[i][0]] == 0) { + for(j = turni[i][0]; j <= turni[i][1]; j++) eDay[j] = 1; + tot++; + } else if(eDay[turni[i][1]] == 0 && turni[i][1] > turni[i+1][0]) { + for(j = turni[i][1]; j >= turni[i][0]; j--) eDay[j] = 1; + tot++; + } + } + + out << tot << endl; + + delete[] eDay; + in.close(); + out.close(); + + return 0; +} -- cgit v1.2.3-18-g5258