Completato esercizio Riempimento (P7.3 nuovo)

This commit is contained in:
Giacomo R. 2023-01-15 17:58:49 +01:00
parent dc9098851c
commit 70740011fe
1 changed files with 35 additions and 0 deletions

View File

@ -0,0 +1,35 @@
public class Riempimento {
public static void main(String[] args) {
boolean[] posti = {false, false, false, false, false, false, false, false, false, false};
int occupati = 0;
while (occupati < posti.length) {
int centro = 0;
int maxSequence = 0;
int actSequence = 0;
for (int i = 0; i < posti.length; i++) {
if (!posti[i]) {
actSequence++;
if (actSequence > maxSequence) {
maxSequence = actSequence;
centro = i - actSequence / 2;
}
} else {
actSequence = 0;
}
}
posti[centro] = true;
occupati++;
for(int i = 0; i < posti.length; i++) {
if(posti[i]) System.out.print("X");
else System.out.print("_");
}
System.out.println();
}
}
}