From a8a40a7703bb3cb337074de011f881a16893f7d1 Mon Sep 17 00:00:00 2001 From: mc-cat-tty <44820563+mc-cat-tty@users.noreply.github.com> Date: Thu, 18 Nov 2021 08:35:32 +0100 Subject: [PATCH] recupera es funzioni --- esercitazioni/gen_primi.cpp | 38 ++++++++++++++++++++ esercitazioni/ric_studenti_iter.cpp | 34 ++++++++++++++++++ esercitazioni/somma_quadrati_ottimizzato.cpp | 30 ++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 esercitazioni/gen_primi.cpp create mode 100644 esercitazioni/ric_studenti_iter.cpp create mode 100644 esercitazioni/somma_quadrati_ottimizzato.cpp diff --git a/esercitazioni/gen_primi.cpp b/esercitazioni/gen_primi.cpp new file mode 100644 index 0000000..00f3046 --- /dev/null +++ b/esercitazioni/gen_primi.cpp @@ -0,0 +1,38 @@ +#include +#include + +using namespace std; + +bool is_primo(const int n) { + if (n>=1 && n<=3) + return true; + if (n%2 == 0) + return false; + for (int i=3; i<=static_cast(sqrt(n)); i+=2) + if (n%i == 0) + return false; + return true; +} + +int prossimo_primo(const int n) { + int i; + for (i=n; !is_primo(i); i++) ; + return i; +} + +int main() { + int a; + do { + cout << "Insersci a: "; + cin >> a; + } while (!(a>0)); + + int np = 0; // numero primo generato dalla fun + while (true) { + np = prossimo_primo(np); + if (np > a) break; + cout << np << endl; + np++; + } + return 0; +} diff --git a/esercitazioni/ric_studenti_iter.cpp b/esercitazioni/ric_studenti_iter.cpp new file mode 100644 index 0000000..6eef5d0 --- /dev/null +++ b/esercitazioni/ric_studenti_iter.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +int calcola_durata(const int tempo_domanda, const int tempo_telefonata, const int intervallo_telefonata) { + int tempo_rimanente_domanda = tempo_domanda; + int tempo_totale = 0; + + do { + if (tempo_totale % intervallo_telefonata == 0) + tempo_totale += tempo_telefonata; // arriva subito una chiamata + else { + tempo_totale++; + tempo_rimanente_domanda--; + } + } while (tempo_rimanente_domanda > 0); + + return tempo_totale; +} + +int main() { + int t_dom, t_tel, delta_tel; + cout << "Tempo totale da dedicare alle domande: "; cin >> t_dom; + cout << "Durata di ogni telefonata: "; cin >> t_tel; + cout << "Frequenza delle telefonate. Ogni x minuti: "; cin >> delta_tel; + + if (t_tel >= delta_tel) { + cout << "Il professore resterĂ  per sempre al telefono" << endl; + } + else { + cout << "Il tempo totale sarĂ : " << calcola_durata(t_dom, t_tel, delta_tel) << endl; + } + return 0; +} diff --git a/esercitazioni/somma_quadrati_ottimizzato.cpp b/esercitazioni/somma_quadrati_ottimizzato.cpp new file mode 100644 index 0000000..7895cce --- /dev/null +++ b/esercitazioni/somma_quadrati_ottimizzato.cpp @@ -0,0 +1,30 @@ +#include +#include + +using namespace std; + +int somma_quadrati(const int n, const int i) { + int j, s; + for (j = i; (s = i*i+j*j) < n; j++) ; + // cout << "\t" << s << "\t" << i << "\t" << j << endl; + return (s == n ? j : -1); +} + +int main() { + int n; + int i, j; + + cout << "n: "; cin >> n; + + j = -1; + for (i=1; i<=int(sqrt(n/2.0)) && j==-1; i++) // i <= int(sqrt(n/2.0)) <==> i*i*2 <= n + j = somma_quadrati(n, i); + i--; + + if (j != -1) + cout << i << " " << j << endl; + else + cout << "Impossibile" << endl; + + return 0; +}