Opzione 1
This commit is contained in:
parent
0314f06fda
commit
cc2d3fc8f4
87
Program.cs
87
Program.cs
@ -17,7 +17,8 @@ class Program {
|
||||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
CreaMatrice();
|
||||
StampaMatriciSeZero(TrovaSottomatriciQuadrate(RiempiMatrice(CreaMatrice())));
|
||||
|
||||
Pausa();
|
||||
break;
|
||||
default:
|
||||
@ -93,16 +94,15 @@ class Program {
|
||||
}
|
||||
} while (error);
|
||||
|
||||
return RiempiMatrice(new int[righe, colonne]);
|
||||
return new int[righe, colonne];
|
||||
}
|
||||
|
||||
static int[,] RiempiMatrice(int[,] p_matrix) {
|
||||
int elemento;
|
||||
for (int r = 0; r < p_matrix.GetLength(0); r++) {
|
||||
for (int c = 0; c < p_matrix.GetLength(1); c++) {
|
||||
Console.Write($"Inserire l'elemento in posizione {r},{c}: ");
|
||||
try {
|
||||
elemento = Convert.ToInt32(Console.ReadLine());
|
||||
p_matrix[r, c] = Convert.ToInt32(Console.ReadLine());
|
||||
}
|
||||
catch (FormatException) {
|
||||
Console.WriteLine("Errore: elemento non valido.");
|
||||
@ -110,7 +110,82 @@ class Program {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return p_matrix;
|
||||
}
|
||||
}
|
||||
|
||||
public static (int, int, int)[]? TrovaSottomatriciQuadrate(int[,] p_matrice) {
|
||||
(int, int, int)[]? ritorno = null, prev = null; //riga iniziale, colonna iniziale e matrice quadrata
|
||||
|
||||
int maxDimensione = (p_matrice.GetLength(0) < p_matrice.GetLength(1)) ? p_matrice.GetLength(0) : p_matrice.GetLength(1); // La massima dimensione possibile di una sottomatrice quadrata
|
||||
|
||||
// Dimensioni delle sottomatrici quadrate: da 2 a maxDimensione
|
||||
for (int dimensione = 2; dimensione <= maxDimensione; dimensione++) {
|
||||
// Riga iniziale
|
||||
for (int i = 0; i <= p_matrice.GetLength(0) - dimensione; i++) {
|
||||
// Colonna iniziale
|
||||
for (int j = 0; j <= p_matrice.GetLength(1) - dimensione; j++) {
|
||||
|
||||
int[,] sottomatrice = new int[dimensione, dimensione];
|
||||
|
||||
for (int r = 0; r < dimensione; r++) {
|
||||
for (int c = 0; c < dimensione; c++) {
|
||||
sottomatrice[r, c] = p_matrice[i + r, j + c];
|
||||
}
|
||||
}
|
||||
|
||||
if (IsMatrixSumZero(sottomatrice) != null) {
|
||||
if (prev == null) {
|
||||
ritorno = new (int, int, int)[1];
|
||||
|
||||
ritorno[0] = (i, j, sottomatrice.GetLength(0));
|
||||
}
|
||||
else {
|
||||
ritorno = new (int, int, int)[prev.Length + 1];
|
||||
|
||||
//copia dell'array vecchio in quello nuovo
|
||||
for (int k = 0; k < prev.Length; k++) {
|
||||
ritorno[k] = prev[k];
|
||||
}
|
||||
|
||||
ritorno[prev.Length] = (i, j, sottomatrice.GetLength(0));
|
||||
}
|
||||
|
||||
prev = ritorno;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ritorno;
|
||||
}
|
||||
|
||||
public static int[,]? IsMatrixSumZero(int[,] p_matrice) { //null se falso, p_matrice se vero
|
||||
int somma = 0;
|
||||
int[,]? ritorno = null;
|
||||
for (int r = 0; r < p_matrice.GetLength(0); r++) {
|
||||
for (int c = 0; c < p_matrice.GetLength(1); c++) {
|
||||
somma += p_matrice[r, c];
|
||||
}
|
||||
}
|
||||
|
||||
if (somma == 0) {
|
||||
ritorno = p_matrice;
|
||||
}
|
||||
return ritorno;
|
||||
}
|
||||
|
||||
static void StampaMatriciSeZero((int, int, int)[]? p_matrici) {
|
||||
if (p_matrici != null) {
|
||||
for (int i = 0; i < p_matrici.Length; i++) {
|
||||
Console.WriteLine("Sottomatrice quadrata a somma nulla trovata:");
|
||||
Console.WriteLine($"\tDimensione: {p_matrici[i].Item3} * {p_matrici[i].Item3}");
|
||||
Console.WriteLine($"\tPosizione iniziale: riga {p_matrici[i].Item1}, colonna {p_matrici[i].Item2}");
|
||||
}
|
||||
}
|
||||
else {
|
||||
Console.WriteLine("Nessuna sottomatrice quadrata a somma nulla trovata.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -13,7 +13,7 @@ using System.Reflection;
|
||||
[assembly: System.Reflection.AssemblyCompanyAttribute("vacanzeEstive_sezione2")]
|
||||
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
||||
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+cafc48ce6407bf16c0d16b4fde5ee8673539a6ca")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+0314f06fda80211ebc1f2be2511a8d52b6b43cad")]
|
||||
[assembly: System.Reflection.AssemblyProductAttribute("vacanzeEstive_sezione2")]
|
||||
[assembly: System.Reflection.AssemblyTitleAttribute("vacanzeEstive_sezione2")]
|
||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||
|
||||
@ -1 +1 @@
|
||||
f51dd8e398f51ce3006057d64eb8817f1c96774015caa399a7ccd91231d29a3e
|
||||
ebb98eae628fd33d11fbc6dc606c0272454799a56e9ea38c60adea94a82bcf21
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user