diff options
author | Santo Cariotti <sancn@live.com> | 2017-04-26 16:37:39 +0200 |
---|---|---|
committer | Santo Cariotti <sancn@live.com> | 2017-04-26 16:37:39 +0200 |
commit | 483d63fa7249ad8d6020680c48c3cf6df35010b3 (patch) | |
tree | 2f8649e3ae6b42ace5011246285c9c450f004222 /cpp | |
parent | 6c957dc4e01aee6ce9cae3c8342d04b0fd9ca9c4 (diff) |
Moved all C++ files into CPP folder
Diffstat (limited to 'cpp')
-rw-r--r-- | cpp/DFS.cpp | 39 | ||||
-rw-r--r-- | cpp/Es 3.cpp | 109 | ||||
-rw-r--r-- | cpp/Es5.cpp | 46 | ||||
-rw-r--r-- | cpp/Es6.cpp | 38 | ||||
-rw-r--r-- | cpp/arrayMinMax.cc | 35 | ||||
-rw-r--r-- | cpp/ascensore.cc | 90 | ||||
-rw-r--r-- | cpp/biblioteca.cc | 57 | ||||
-rw-r--r-- | cpp/bicicletta.c++ | 62 | ||||
-rw-r--r-- | cpp/borsa.cc | 32 | ||||
-rw-r--r-- | cpp/carte.cpp | 105 | ||||
-rw-r--r-- | cpp/conversioneBinariaDec.cc | 42 | ||||
-rw-r--r-- | cpp/crittografia.cc | 89 | ||||
-rw-r--r-- | cpp/es4.cpp | 67 | ||||
-rw-r--r-- | cpp/grafo.cpp | 73 | ||||
-rw-r--r-- | cpp/hanoi.cc | 29 | ||||
-rw-r--r-- | cpp/lswf.cpp | 67 | ||||
-rw-r--r-- | cpp/numeri.cc | 50 | ||||
-rw-r--r-- | cpp/palindromo.cc | 25 | ||||
-rw-r--r-- | cpp/parentesi.cpp | 58 | ||||
-rw-r--r-- | cpp/planet.cc | 72 | ||||
-rw-r--r-- | cpp/scommesse.cpp | 81 | ||||
-rw-r--r-- | cpp/sorveglianza.c++ | 88 | ||||
-rw-r--r-- | cpp/substring.cc | 22 |
23 files changed, 1376 insertions, 0 deletions
diff --git a/cpp/DFS.cpp b/cpp/DFS.cpp new file mode 100644 index 0000000..fda5a33 --- /dev/null +++ b/cpp/DFS.cpp @@ -0,0 +1,39 @@ +#include <iostream>
+#define BIANCO 0 //Non Visitato
+#define NERO 2 //Visitato
+#define GRIGIO 1 //Non Finito
+#define INF 999999
+#include <vector>
+#include <fstream>
+#include <queue>
+#define MAXN 9999
+#define INDEF -1
+#include <stack>
+using namespace std;
+int V=9;
+int matriceAdj[MAXN][MAXN];
+ int colore[MAXN];
+ //DFS Tramite matrice di adiacenza
+ void dfsVisit (int u)
+ {
+ colore[u]= GRIGIO;
+ for (int i=0;i<V;i++)
+ {
+ if (matriceAdj[u][i] == 1 && colore[i] == BIANCO )
+ dfsVisit(i);
+ }
+ colore[u]= NERO;
+ }
+ // Le chiamate ricorsive sono quanti sono i nodi GRIGI
+ int dfs()
+ {
+ for (int i=0;i<V;i++) colore[i]=BIANCO;
+ for (int i=0;i<V;i++) if (colore[i]==BIANCO) dfsVisit(i);
+ }
+
+int main(int argc, char** argv) {
+
+ //Vanno settati i dati riguardanti gli archi esistenti
+
+ return 0;
+}
diff --git a/cpp/Es 3.cpp b/cpp/Es 3.cpp new file mode 100644 index 0000000..d525f62 --- /dev/null +++ b/cpp/Es 3.cpp @@ -0,0 +1,109 @@ +#include <iostream>
+#define BIANCO 0 //Non Visitato
+#define NERO 2 //Visitato
+#define GRIGIO 1 //Non Finito
+#define INF 999999
+#include <vector>
+#include <fstream>
+#include <queue>
+#define MAXN 9999
+#define INDEF -1
+#include <stack>
+using namespace std;
+typedef pair <int,int> p;
+std::priority_queue<p, std::vector<p>, std::greater<p> > Q;
+int V,E;
+int matriceAdj[MAXN][MAXN];
+int T;
+struct nodo {
+ vector <int> adj;
+ int inizio,fine;
+}no[MAXN];
+ int colore[MAXN];
+ bool Ciclico=false;
+ bool dfsVisit (int u)
+ {
+ cout<<u<<endl;
+ colore[u]= GRIGIO;
+ int inizio=++T;
+ no[u].inizio=inizio;
+ int a;
+ for (int i=0;i<no[u].adj.size();i++)
+ {
+ a=no[u].adj[i];
+ if (colore[a]==GRIGIO)
+ {
+ Ciclico=true;
+ return 1;
+ }
+ if ( colore[a] == BIANCO )
+ {
+ int b=dfsVisit(a);
+ if (b)
+ {
+ return 1;
+ }
+ }
+ }
+ colore[u]= NERO;
+ int fine=++T;
+ no[u].fine=fine;
+ Q.push(p(fine,inizio));
+ return 0;
+ }
+
+ void dfsVisit2 (int u)
+ {
+ cout<<u<<endl;
+ colore[u]= GRIGIO;
+ int a;
+ for (int i=0;i<no[u].adj.size();i++)
+ {
+ a=no[u].adj[i];
+ if ( colore[a] == BIANCO )
+ dfsVisit(a);
+ }
+ colore[u]= NERO;
+ }
+ // Le chiamate ricorsive sono quanti sono i nodi GRIGI
+ void dfs()
+ {
+ for (int i=0;i<V;i++) colore[i]=BIANCO;
+ for (int i=0;i<V;i++) if (colore[i]==BIANCO) if (dfsVisit(i)) return;
+ }
+
+
+int main(int argc, char** argv) {
+ ifstream in ("input.txt");
+ ofstream out ("output.txt");
+ in>>V>>E;
+ int a,b;
+ for (int i=0;i<E;i++)
+ {
+ in>>a>>b;
+ no[a].adj.push_back(b);
+ }
+ dfs();
+ for (int i=0;i<V;i++)
+ {
+ cout<<"nodo "<<i<<endl;
+ cout<<"Inizio "<<no[i].inizio<<" Fine "<<no[i].fine<<endl;
+ }
+ if (!Ciclico)
+ {
+ for (int i=0;i<V;i++) colore[i]=BIANCO;
+ b=0;
+ while (!Q.empty())
+ {
+ a=Q.top().first;
+ Q.pop();
+ if (colore[a]==BIANCO)
+ {
+ dfsVisit2(a);
+ b++;
+ }
+ }
+ cout<<"Il numero di Componenti connesse e' "<<b;
+ }
+ return 0;
+}
diff --git a/cpp/Es5.cpp b/cpp/Es5.cpp new file mode 100644 index 0000000..8f6fb48 --- /dev/null +++ b/cpp/Es5.cpp @@ -0,0 +1,46 @@ +#include <iostream>
+#define BIANCO 0 //Non Visitato
+#define NERO 2 //Visitato
+#define GRIGIO 1 //Non Finito
+#define INF 999999
+#include <vector>
+#include <fstream>
+#include <queue>
+#define MAXN 9999
+#define INDEF -1
+#include <stack>
+using namespace std;
+typedef pair <int,int> p;
+struct nodo{
+ vector<int> adj;
+ vector<int> p;
+};
+
+int main(int argc, char** argv) {
+ int v,e;
+ int sorgente;
+ nodo no[v];
+
+ priority_queue<p> Q;
+ int peso[v];
+ Q.push(p(0,sorgente));
+
+ while(!Q.empty())
+ {
+ pair <int,int> a=Q.top();
+ Q.pop();
+ if (a.first > peso[a.second])
+ continue;
+
+ for (int i=0;i<no[a.second].adj.size();i++)
+ {
+ if (peso[no[a.second].adj[i]]>a.first+ no[a.second].p[i])
+ {
+ peso[no[a.second].adj[i]]=a.first + no[a.second].p[i];
+ Q.push(p(peso[no[a.second].adj[i]],no[a.second].adj[i]));
+ }
+ }
+
+ }
+ return 0;
+}
diff --git a/cpp/Es6.cpp b/cpp/Es6.cpp new file mode 100644 index 0000000..9907eb9 --- /dev/null +++ b/cpp/Es6.cpp @@ -0,0 +1,38 @@ +#include <iostream>
+#include <algorithm>
+using namespace std;
+/* run this program using the console pauser or add your own getch, system("pause") or input loop */
+
+int main(int argc, char** argv) {
+ int v=5;
+ int matrice[v][v];
+ for (int i=0;i<v;i++)
+ {
+ for (int j=0;j<v;j++)
+ matrice[i][j]=9999;
+ }
+ for (int i=1;i<v;i++) matrice[i][i]=0;
+ matrice[1][4]=5;
+ matrice[1][2]=1;
+ matrice[2][3]=2;
+ matrice[3][1]=2;
+ matrice[3][4]=1;
+ matrice[4][1]=3;
+ for (int h=1;h<v;h++)
+ {
+ for (int i=1;i<v;i++)
+ {
+ for (int j=1;j<v;j++)
+ {
+ matrice[i][j]=min(matrice[i][j],matrice[i][h]+matrice[h][j]);
+ }
+ }
+ }
+ for (int i=1;i<v;i++)
+ {
+ for (int j=1;j<v;j++)
+ cout<<matrice[i][j]<<" ";
+ cout<<endl;
+ }
+ return 0;
+}
diff --git a/cpp/arrayMinMax.cc b/cpp/arrayMinMax.cc new file mode 100644 index 0000000..be36903 --- /dev/null +++ b/cpp/arrayMinMax.cc @@ -0,0 +1,35 @@ +#include <iostream> + +using namespace std; + +int main(void) +{ + int a[] = {5,14,9,8,10,65,32,1,6,78}; + int tot = sizeof a / sizeof(int); + int t, i, j, minore = a[0], maggiore = a[tot-1]; + + for(i = 0; i < tot; i++) + { + for(j = 0; j < tot-1; j++) + { + if(a[j] > a[j+1]){ + t = a[j+1]; + a[j+1] = a[j]; + a[j] = t; + } + } + } + + //for(i = 0; i < tot; i++) cout << a[i] << endl; + + /*for(int i = 0; i < tot; i++) + { + if(a[i] < minore) minore = a[i]; + else if(a[i] > maggiore) maggiore = a[i]; + }*/ + + cout << minore << endl; + cout << maggiore << endl; + + return 0; +} diff --git a/cpp/ascensore.cc b/cpp/ascensore.cc new file mode 100644 index 0000000..8e69964 --- /dev/null +++ b/cpp/ascensore.cc @@ -0,0 +1,90 @@ +#include <iostream> +#include <stdlib.h> +#include <string> +#include <unistd.h> + +class Ascensore +{ +private: + unsigned short int floor; + bool doors; + unsigned short int maxFloor; +public: + Ascensore(unsigned short int plan = 0) {floor = plan; maxFloor = 5;} + void Up(void); + void Down(void); + void Doors(void); + void getFloor(void); +}; + +void _clear(void); + +using namespace std; + +int main(void) +{ + char choose; + Ascensore *ascensore = new Ascensore(); + do { + ascensore->getFloor(); + cin >> choose; + choose = tolower(choose); + if(choose == 'u' || choose == 'd') ascensore->Doors(); + sleep(2); + switch(choose) + { + case 'u' : ascensore->Up(); break; + case 'd' : ascensore->Down(); break; + case 'e' : break; + } + _clear(); + if(choose == 'u' || choose == 'd') ascensore->Doors(); + } while(choose != 'e'); + + cout << "SEI USCITO DALL'ASCENSORE CON SUCCESSO!"; + + return 0; +} + +void _clear(void) +{ + #ifdef OS_WINDOWS + system("CLS"); + #else + system("clear"); + #endif +} + +void Ascensore::getFloor(void) +{ + if(floor == maxFloor) cout << "Sei arrivato all'ultimo piano" << endl; + if(floor == 0) cout << "Non puoi scendere più di così" << endl; + cout << "SEI ATTUALMENTE AL PIANO " << floor << endl; + cout << "\nQUALE AZIONE SI VUOLE COMPIERE?" << endl; + cout << "U - salire" << endl; + cout << "D - scendere" << endl; + cout << "E - Uscire" << endl; + cout << "> "; +} + +void Ascensore::Up(void) +{ + if(floor < maxFloor) floor+=1; +} + +void Ascensore::Down(void) +{ + if(floor > 0) floor-=1; +} + +void Ascensore::Doors(void) +{ + if(doors == true) + { + cout << "> APERTURA PORTE" << endl; + doors = false; + } else { + cout << "> CHIUSURA PORTE..." << endl; + doors = true; + } +} diff --git a/cpp/biblioteca.cc b/cpp/biblioteca.cc new file mode 100644 index 0000000..85cd80f --- /dev/null +++ b/cpp/biblioteca.cc @@ -0,0 +1,57 @@ +#include <iostream> +#include <fstream> +#include <sstream> +#include <time.h> + +using namespace std; + +int main(void) +{ + ifstream filein; + ofstream fileout; + + filein.open("input.txt"); + fileout.open("output.txt"); + + time_t rawtime; + struct tm * timeinfo; + + int num[4], i = 0, j = 1, diff, tot[2]; + char ch; + string cc; + + while(!filein.eof()) + { + filein.get(ch); + if(ch != ' '){ + cc+=ch; + istringstream(cc) >> num[i]; + }else { + i++; + cc = ""; + } + } + + time(&rawtime); + timeinfo = localtime(&rawtime); + + for(i = 0; i < 2; i++){ + timeinfo->tm_year = 2001-1900; + timeinfo->tm_mon = num[j]-1; + timeinfo->tm_mday = num[j-1]; + + mktime(timeinfo); + + tot[i] = timeinfo->tm_yday+1; + j = 3; + } + + diff = tot[1] - tot[0]; + + fileout << diff; + + filein.close(); + fileout.close(); + + return 0; +} diff --git a/cpp/bicicletta.c++ b/cpp/bicicletta.c++ new file mode 100644 index 0000000..bb42031 --- /dev/null +++ b/cpp/bicicletta.c++ @@ -0,0 +1,62 @@ +/* INPUT: +3 4 +2 +3 +1 +1 3 +1 2 +3 2 +3 1 + +OUTPUT: +3 +*/ + +#include <iostream> +#include <fstream> + +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/cpp/borsa.cc b/cpp/borsa.cc new file mode 100644 index 0000000..05fdd85 --- /dev/null +++ b/cpp/borsa.cc @@ -0,0 +1,32 @@ +#include <iostream> + +using namespace std; + +int main(void) +{ + int a[] = {13,24,7,8,6,15,2,21,17,7,3,19,20,1}; + int minore, diffp, diff = 0, x, y; + int tot = sizeof a/sizeof(int); + + for(int i = 0; i < tot; i++) + { + minore = a[i]; + + for(int j = i; j < tot-1; j++) + { + if(a[j] == minore) continue; + else if(a[j] > minore) diffp = a[j] - minore; + if(diffp > diff && diffp > 0) { + x = i; + y = j; + diff = diffp; + } + } + } + + cout << "Ti conviene comprare a " << a[x] << " giorno " << x+1 << " e vendere a " << a[y] << " giorno " << y+1 << endl; + cout << "Il tuo guadagno sarebbe: " << diff; + + + return 0; +} diff --git a/cpp/carte.cpp b/cpp/carte.cpp new file mode 100644 index 0000000..697622a --- /dev/null +++ b/cpp/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 <iostream> +#include <fstream> + +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/cpp/conversioneBinariaDec.cc b/cpp/conversioneBinariaDec.cc new file mode 100644 index 0000000..e88a2fb --- /dev/null +++ b/cpp/conversioneBinariaDec.cc @@ -0,0 +1,42 @@ +#include <iostream> +#include <math.h> +#include <string.h> + +using namespace std; + +const int b = 32; + +int main() +{ + int v[b] = {0}; //tutti i valori sono uguali a 0 + char str[b]; + int valVuoto, i, j; + int num = 0; + + cin >> str; + /* se la lunghezza della stringa è inferiore a quella della base, indica da quale punto è vuota */ + if(strlen(str) < b) + valVuoto = strlen(str); + + /* ordina i valori in modo tale da spostarli a blocco, ovvero entrambi gli indici si incrementano + man mano che ordinano il vettore degli interi. La base - la prima X di 'str' è l'inizio dell'ordine + del vettore degli int. + + Seguendo questa logica e questo esempio: + STR - + 1 0 x x x x x x + + V - + x x x x x x 1 0 + */ + for(i = b - valVuoto, j = 0; i < b; i++, j++) + v[i] = str[j] - '0'; + + //il vettore viene letto in ordine decrescente, mentre le potenze in crescente + for(i = b - 1, j = 0; i > -1; i--, j++) + num += v[i] * pow(2, j); + + cout << num << endl; + + return 0; +} diff --git a/cpp/crittografia.cc b/cpp/crittografia.cc new file mode 100644 index 0000000..d448b86 --- /dev/null +++ b/cpp/crittografia.cc @@ -0,0 +1,89 @@ +#include <iostream> +#include <string> +#include <sstream> +#include <fstream> +#include <cstring> + +using namespace std; + +void converti(char *str, int size); +void vocalic(char *str, int size); + +char consonantiMin[] = {'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','z'}; +char consonantiMax[] = {'B','C','D','F','G','H','J','K','L','M','N','P','Q','R','S','T','V','W','X','Z'}; + +int main() +{ + ifstream in; + ofstream out; + + in.open("input.txt"); + out.open("output.txt"); + + int N; + string ca; + + getline(in, ca); + istringstream(ca) >> N; + + for(int i = 0; i < N; i++){ + getline(in, ca); + char *c = new char[ca.length() + 1]; + strcpy(c, ca.c_str()); + converti(c, ca.length() + 1); + out << c << endl; + delete[] c; + } + + in.close(); + out.close(); + + return 0; +} + +void vocalic(char *str, int i) +{ + if(str[i] == 'a') str[i] = 'e'; + else if(str[i] == 'e') str[i] = 'i'; + else if(str[i] == 'i') str[i] = 'o'; + else if(str[i] == 'o') str[i] = 'u'; + else if(str[i] == 'u') str[i] = 'y'; + else if(str[i] == 'y') str[i] = 'a'; + else if(str[i] == 'A') str[i] = 'E'; + else if(str[i] == 'E') str[i] = 'I'; + else if(str[i] == 'I') str[i] = 'O'; + else if(str[i] == 'O') str[i] = 'U'; + else if(str[i] == 'U') str[i] = 'Y'; + else if(str[i] == 'Y') str[i] = 'A'; + else if(str[i] == 'z') str[i] = 'b'; + else if(str[i] == 'Z') str[i] = 'B'; + else if(str[i] == '0') str[i] = '1'; + else if(str[i] == '1') str[i] = '2'; + else if(str[i] == '2') str[i] = '3'; + else if(str[i] == '3') str[i] = '4'; + else if(str[i] == '4') str[i] = '5'; + else if(str[i] == '5') str[i] = '6'; + else if(str[i] == '6') str[i] = '7'; + else if(str[i] == '7') str[i] = '8'; + else if(str[i] == '8') str[i] = '9'; + else if(str[i] == '9') str[i] = '0'; +} + +void converti(char *str, int size) +{ + bool cpres = false; + for(int i = 0; i < size-1; i++){ + int s=1; + vocalic(str,i); + for(unsigned int j = 0; j < sizeof consonantiMin; j++){ + if(str[i] == consonantiMin[j] || str[i] == consonantiMax[j]){ + cpres = true; + if(str[i]+1 == 'A' || str[i]+1 == 'E' || str[i]+1 == 'I' || str[i]+1 == 'O' || str[i]+1 == 'U' || str[i]+1 == 'Y' || + str[i]+1 == 'a' || str[i]+1 == 'e' || str[i]+1 == 'i' || str[i]+1 == 'o' || str[i]+1 == 'u' || str[i]+1 == 'y') s = 2; + else s = 1; + } + } + if(cpres == true) str[i]+=s; + cpres = false; + } +} diff --git a/cpp/es4.cpp b/cpp/es4.cpp new file mode 100644 index 0000000..a1f1716 --- /dev/null +++ b/cpp/es4.cpp @@ -0,0 +1,67 @@ +#include <iostream>
+#define BIANCO 0 //Non Visitato
+#define NERO 2 //Visitato
+#define GRIGIO 1 //Non Finito
+#define INF 999999
+#include <vector>
+#include <fstream>
+#include <queue>
+#define MAXN 9999
+#define INDEF -1
+#include <stack>
+using namespace std;
+typedef pair<int,int> p;
+int V,E;
+
+struct nodo {
+ vector <p> adj;
+ int dist;
+ nodo(){
+ dist=INF;
+ }
+}no[MAXN];
+ int colore[MAXN];
+ void relax (nodo no[], int u, int z)
+ {
+ p v= no[u].adj[z];
+ if (no[v.first].dist> no[u].dist + v.second )
+ {
+ no[v.first].dist= no[u].dist + v.second;
+ }
+ }
+
+ void bf (nodo no[],int n, int s)
+ {
+ int i,j,z;
+ no[0].dist=0;
+ for (i=0;i<V;i++) //Scorro
+ {
+ for (j=0;j<V;j++)
+ {
+ for (z=0;z<no[j].adj.size();z++)
+ {
+ relax(no,j,z);
+ }
+ }
+ }
+
+ }
+
+
+int main(int argc, char** argv) {
+ ifstream in ("input.txt");
+ ofstream out ("output.txt");
+ in>>V>>E;
+ int a,b,c;
+ for (int i=0;i<E;i++)
+ {
+ in>>a>>b>>c;
+ no[a].adj.push_back(p(b,c));
+ }
+ bf(no,V,0); //Adiacenze, numeri di vertici, sorgente
+ for (int i=0;i<V;i++)
+ {
+ cout<<no[i].dist<<endl;
+ }
+ return 0;
+}
diff --git a/cpp/grafo.cpp b/cpp/grafo.cpp new file mode 100644 index 0000000..911d7e6 --- /dev/null +++ b/cpp/grafo.cpp @@ -0,0 +1,73 @@ +#include <iostream>
+#define BIANCO 0 //Non Visitato
+#define NERO 1 //Visitato
+#define GRIGIO //Non Finito
+#define INF 999999
+#include <vector>
+#include <fstream>
+#include <queue>
+#define MAXN 9999
+#define INDEF -1
+#include <stack>
+using namespace std;
+int V=9; //Nodi
+int precedente[MAXN]; //Vettore su cui viene salvato il percorso per arrivare alla destinazione
+struct nodo {
+ vector <int> adj;
+}no[MAXN];;
+
+ int bfsVisit (int s)
+ {
+ int dist[V];
+ int colore[V];
+ queue <int> q;
+
+ for (int i=0;i<V;i++)
+ {
+ precedente[i]=-1;
+ dist[i]=INF;
+ colore[i]=BIANCO;
+ }
+ dist[s]=INF;
+ colore[s]=NERO;
+ q.push(s);
+ while (!q.empty())
+ {
+ int a=q.front();
+ q.pop();
+ for (int i=0;i<no[a].adj.size();i++)
+ {
+ int b=no[a].adj[i];
+ if (colore[b] == BIANCO)
+ {
+ q.push(b);
+ colore[b]=NERO;
+ precedente[b]=a;
+ dist[b]=dist[a] +1;
+ }
+ }
+ }
+ /*
+ stack <int> sta;
+ int i=3; // arrivo
+ sta.push(i);
+ while (precedente[i]!=INDEF)
+ {
+
+ }
+ */ //Procedura iterativa per stampare il percorso fino al nodo sorgente
+
+ }
+
+ void stampaPercorso (int j) //Procedura ricorsiva j=precedente[destinazione]
+ {
+ if (precedente[j] != INDEF )
+ stampaPercorso(precedente[j]);
+ cout<<j;
+ }
+int main(int argc, char** argv) {
+
+
+
+ return 0;
+}
diff --git a/cpp/hanoi.cc b/cpp/hanoi.cc new file mode 100644 index 0000000..b401060 --- /dev/null +++ b/cpp/hanoi.cc @@ -0,0 +1,29 @@ +// ConsoleApplication1.cpp : definisce il punto di ingresso dell'applicazione console. +// +#include "stdafx.h" +#include <iostream> + +using namespace std; + +static unsigned long int tot = 0; + +void hanoi(int d, int inizio, int fine, int transito) +{ + if (d == 1); + else { + hanoi(d - 1, inizio, transito, fine); + hanoi(d - 1, transito, fine, inizio); + } + tot++; +} + +int main() +{ + int dischi; + cout << "Numero dischi: "; + cin >> dischi; + + hanoi(dischi, 1, 3, 2); + cout << "In totale: " << tot << " mosse" << endl; + return 0; +} diff --git a/cpp/lswf.cpp b/cpp/lswf.cpp new file mode 100644 index 0000000..218a9c8 --- /dev/null +++ b/cpp/lswf.cpp @@ -0,0 +1,67 @@ +/* INPUT: + * 19 + * + * OUTPUT: + * 1000101 + */ +#include <iostream> +#include <fstream> +#define MAXG 1000 + +using namespace std; + +int fibonacci(int* fib, int N) +{ + fib[0] = 1; fib[1] = 1; + int lst; + + for(int i = 2; i < N; i++) { + fib[i] = fib[i-1] + fib[i-2]; + lst = i; + if(fib[i] > N) break; + } + + return lst; +} + +int main() +{ + ifstream in("input.txt"); + ofstream out("output.txt"); + + int N, i; + in >> N; + int caracts[MAXG], somma = 0, potSomma; + int lastc; + if(N > 4) + lastc = fibonacci(caracts, N); + else { + caracts[0] = 1; caracts[1] = 1; caracts[2] = 2; caracts[3] = 3; + if(N == 1) lastc = 1; + else if(N == 2) lastc = 2; + else if(N == 3) lastc = 3; + else lastc = 4; + } + int* seq = new int[lastc]; + +<<<<<<< HEAD + for(i = 0; i < lastc; i++) cout << caracts[i] << ' '; cout << endl; +======= + for(i = 0; i < lastc; i++) cout << caracts[i] << endl; cout << endl; +>>>>>>> devs + + seq[0] = 1; + for(i = lastc; i > 0; i--) { + potSomma = somma + caracts[i]; + if(potSomma < N) { + somma = potSomma; + seq[i] = 1; + } else seq[i] = 0; + } + for(i = 0; i < lastc; i++) out << seq[i]; + + delete[] seq; + in.close(); + out.close(); + return 0; +} diff --git a/cpp/numeri.cc b/cpp/numeri.cc new file mode 100644 index 0000000..ec2d2d4 --- /dev/null +++ b/cpp/numeri.cc @@ -0,0 +1,50 @@ +#include <iostream> +#include <fstream> + +using namespace std; + +int main(void) +{ + fstream file; + file.open("dati.dat"); + + typedef unsigned short int size_t; + + size_t a, b, c, seq = 0, tot = 0, num; + char stringa[] = "12312312312"; + char l; + while(true) + { + cout << "3 numeri: "; + cin >> a >> b >> c; + if(a >= 0 && a <= 9 && b >= 0 && b <= 9 && c >= 0 && c <= 9){ + if(a != b && b != c && a != c) break; + } + } + + file << a << endl; + file << b << endl; + file << c << endl; + file << stringa << endl; + file.seekg(5, file.beg); + + while(!file.eof()) + { + file.get(l); + num = l - '0'; + if(seq == 0) { + if(num == a) seq++; + } else if(seq == 1) { + if(num == b) seq++; + } else if(seq == 2) { + if(num == c) seq++; + } + + if(seq == 3) { tot++; seq = 0; } + } + + cout << "\n" << tot; + file.close(); + + return 0; +} diff --git a/cpp/palindromo.cc b/cpp/palindromo.cc new file mode 100644 index 0000000..1cb19a2 --- /dev/null +++ b/cpp/palindromo.cc @@ -0,0 +1,25 @@ +// ConsoleApplication1.cpp : definisce il punto di ingresso dell'applicazione console.
+//
+#include "stdafx.h"
+#include <iostream>
+
+using namespace std;
+
+bool palindromo(int i, int j, char str[])
+{
+ if (i >= j) return true;
+ else if (str[i] == str[j]) return palindromo(i + 1, j - 1, str);
+ else return false;
+}
+
+int main()
+{
+ char parola[] = "onorarono";
+
+ cout << ( (palindromo(0, sizeof parola - 2, parola) == 1) ? "E' palindromo" : "Non e' palindromo" ) << endl;
+
+ cin.get();
+
+ return 0;
+}
+
diff --git a/cpp/parentesi.cpp b/cpp/parentesi.cpp new file mode 100644 index 0000000..159b75f --- /dev/null +++ b/cpp/parentesi.cpp @@ -0,0 +1,58 @@ +/* INPUT: +[ ( ( [ { { [ ] ] ( ( ( } } ( ) ) ) ) ) ) ] ] ] + +output: +11 */ +#include <iostream> +#include <string.h> +#include <fstream> + +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/cpp/planet.cc b/cpp/planet.cc new file mode 100644 index 0000000..33cdea7 --- /dev/null +++ b/cpp/planet.cc @@ -0,0 +1,72 @@ +#include <iostream> +#include <fstream> +#include <sstream> + +using namespace std; + +int main() +{ + ifstream in; + ofstream out; + in.open("input.txt"); + out.open("output.txt"); + + char ch; + string numero, val[2]; + int oraVuota = -1, giorno[96], i = 0, c = 0; + + for(int j = 0; j < 96; j++) giorno[j] = -1; + + getline(in, numero); + int N; + istringstream(numero) >> N; + + int ore[N][2]; + + while(!in.eof()) + { + in.get(ch); + + if(ch == '\n') { + val[0] = ""; + val[1] = ""; + i = 0; + c++; + + continue; + }else if(ch == ' '){ + i++; + }else { + val[i] += ch; + istringstream(val[i]) >> ore[c][i]; + } + + } + + i = 0; + while(i < N){ + if(ore[i][0] > ore[i][1]){ + for(int j = ore[i][0]; j < ore[i][1] || j < 96; j++) giorno[j] = 0; + for(int k = ore[i][1]-1; k >= 0; k--) giorno[k] = 0; + }else { + for(int k = ore[i][0]; k < ore[i][1]; k++) giorno[k] = 0; + } + i++; + } + + for(i = 0; i < 96; i++) { + if(giorno[i] == -1) { + oraVuota = i; + break; + } + + //cout << i << " " << giorno[i] << endl; + } + + out << oraVuota << endl; + + in.close(); + out.close(); + + return 0; +} diff --git a/cpp/scommesse.cpp b/cpp/scommesse.cpp new file mode 100644 index 0000000..2fa0936 --- /dev/null +++ b/cpp/scommesse.cpp @@ -0,0 +1,81 @@ +/* INPUT: + * 11 + * 1 0 2 6 4 5 3 9 8 10 7 + * + * OUTPUT: + * 2 + * 8 2 + */ +#include <iostream> +#include <fstream> +#include <vector> +#include <algorithm> + +using namespace std; + +vector<int> carteOut; + +void cOutPresente(vector<int>& kr, int x) +{ + bool s = false; + for(unsigned i = 0; i < kr.size(); i++) + if(kr[i] == x) s = true; + + if(!s) + kr.push_back(x); +} + +void fCarte(vector<int> c, int N, int s) +{ + int i; + vector<int>::iterator it; + vector<int>::iterator start; + + while(c.size() > 1) { + //for(it = c.begin(); it != c.end(); it++) cout << *it << ' '; cout << endl; + int tot = c.size(); + for(i = s; i < tot-1; i++) { + if(!(((c[i] + c[i+1]) % 2) == 0)) { + start = c.begin()+i; + c.erase(start, start+2); + break; + } + } + + s = 0; + } + cOutPresente(carteOut, c[0]); +} + +int main() +{ + ifstream in("input.txt"); + ofstream out("output.txt"); + int N, i; + in >> N; + + int x; + vector<int> carteV; + + for(i = 0; i < N; i++) { + in >> x; + carteV.push_back(x); + } + + for(int i = 0; i < N; i++) { + fCarte(carteV, N, i); + } + + reverse(carteV.begin(), carteV.end()); + + for(int i = 0; i < N; i++) { + fCarte(carteV, N, i); + } + + out << carteOut.size() << endl; + for(vector<int>::iterator ite = carteOut.begin(); ite != carteOut.end(); ite++) out << *ite << ' '; + + out.close(); + in.close(); + return 0; +} diff --git a/cpp/sorveglianza.c++ b/cpp/sorveglianza.c++ new file mode 100644 index 0000000..4154596 --- /dev/null +++ b/cpp/sorveglianza.c++ @@ -0,0 +1,88 @@ +/* INPUT: +10 +6 +2 5 +0 2 +1 3 +5 6 +4 7 +7 9 + +OUTPUT: +4*/ +#include <iostream> +#include <vector> +#include <fstream> + +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<int> > turni(turniTot, vector<int>(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; +} diff --git a/cpp/substring.cc b/cpp/substring.cc new file mode 100644 index 0000000..55aa319 --- /dev/null +++ b/cpp/substring.cc @@ -0,0 +1,22 @@ +#include <iostream> + +using namespace std; + +void s(int i, int k, int n, char str[]); + +int main() +{ + char ss[] = "SANTO"; + s(0, 4, sizeof(ss), ss); + + return 0; +} + +void s(int i, int k, int n, char str[]) +{ + if(i < n - k){ + if(i < n-k) for(int j = i; j < i+k; j++) cout << str[j]; + cout << "\n"; + s(i+=1, k, n, str); + } +} |