Created
May 18, 2024 02:18
-
-
Save ulisseshen/8545c41526a171b196f0bc9d894b97a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
final balde3L = Balde(3); | |
final balde5L = Balde(5); | |
const int litrosDesejado = 4; | |
while (balde3L.litrosPreenchidos + balde5L.litrosPreenchidos != litrosDesejado) { | |
if(balde3L.capacidade >= litrosDesejado){ | |
balde3L.encher(); | |
continue; | |
} | |
if (balde5L.litrosPreenchidos == 0) { | |
// Encher o balde de 5 litros | |
balde5L.encher(); | |
} else if (balde3L.litrosPreenchidos != balde3L.capacidade) { | |
// Transferir do balde de 5 litros para o balde de 3 litros | |
balde5L.transferirPara(balde3L); | |
} else { | |
// Esvaziar o balde de 3 litros | |
balde3L.esvaziar(); | |
print('Empty B3: $balde3L'); | |
} | |
} | |
if (balde3L.capacidade == litrosDesejado){ | |
print('Resultado final: $balde3L'); | |
} else { | |
print('Resultado final: $balde5L'); | |
} | |
} | |
class Balde { | |
final int capacidade; | |
double litrosPreenchidos = 0.0; | |
Balde(this.capacidade); | |
void encher() { | |
litrosPreenchidos = capacidade.toDouble(); | |
print('Fill B$capacidade: $this'); | |
} | |
void esvaziar() { | |
litrosPreenchidos = 0.0; | |
} | |
void transferirPara(Balde outroBalde) { | |
final espacoDisponivel = outroBalde.capacidade - outroBalde.litrosPreenchidos; | |
final quantidadeTransferida = (litrosPreenchidos < espacoDisponivel) ? litrosPreenchidos : espacoDisponivel; | |
litrosPreenchidos -= quantidadeTransferida; | |
outroBalde.litrosPreenchidos += quantidadeTransferida; | |
print('B$capacidade >> B${outroBalde.capacidade}: $this, $outroBalde'); | |
} | |
@override | |
String toString() => 'Balde${capacidade}L(L: $capacidade, Fill: $litrosPreenchidos)'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment