Jump to content
Dubfx

Sistem discount de sarbatori.

Recommended Posts

Se da un magazin online pe care se vand sa spunem ... cutite de bucatarie.

Toate cutitele AU ACELASI PRET 99 RON.

La adaugarea in cos se aplica un discount astfel:

- la 2 produse comandate pretul total o sa fie de 149 lei

- la 3 produse comandate pretul total o sa fie de 199 lei

- la 4 produse comandate pretul total o sa fie de 199 (grupul de 3 produse reduse) + 99 lei (al patrulea produs la pret intreg) = 298 lei

- la 5 produse comandate pretul total o sa fie de 199 (grupul de 3 produse reduse) + 149 lei (grupul de 2 produse cu discount) = 348 lei

.... so on.

Care este algoritmul?

HINT: repet, toate produsele au acelasi pret.

P.S. Un an nou fericit!

Link to comment
Share on other sites

Ceva facut rapid.


<?php
$pret1 = 99;
$pret2 = 149;
$pret3 = 199;

$cant = 9;
$pret_final = 0;
while($cant > 0) {
if($cant - 3 >= 0) {
$cant -= 3;
$pret_final += $pret3;
} else if($cant - 2 >= 0) {
$cant -= 2;
$pret_final += $pret2;
} else {
$cant -= 1;
$pret_final += $pret1;
}
}
echo $pret_final
?>

Link to comment
Share on other sites

Sau, alta varianta mai optimizata un pic


<?php
$preturi = array(1 => 99, 2 => 149, 3 => 199);

$cant = 5;
$pret_final = 0;
for($i=3; $i >= 1;$i--) {
while($cant >= $i) {
$cant = $cant - $i;
$pret_final += $preturi[$i];
}
}
echo $pret_final
?>

Inspirat din algoritmul descompunerii in factori primi.

Edited by GarryOne
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...