Logica Programming

0

I need if you can help me with a simple algorithm in whatever language, I have the following I have a existencia=3 , I have a two rows that have saldo=1 , saldo=3 , precio_uni=154.16 , precio_uni=116 .

Well what I have to do is go through saldo that have 1 and since it is not equal to existencia I pass the 154.16 of precio , then I pass the second saldo that has 3 but to equal the 3 of existencia I only occupy 2 of saldo and I raise the precio I wanted to add twice 116 to give me 154.16 + 116 + 116 and like this already the saldos with the existencia

If someone can help me with logic

    
asked by Javier Solis 10.08.2018 в 03:50
source

2 answers

3

Trying to understand what you say to me, I think you need this:

//el lenguaje es php
<?php
$existencia=3;
$saldo1= 1;
$precio1=154.16;
$saldo2= 3;
$precio2=116;



//corremos hasta saldo mayor
for (j=0; j < $saldo2;j++) 
{
//vamos a sumar si el es menor o igual a la existencia
    if ($saldo1 <= $existencia) {
        $saldo1 = $saldo1 + 1;
        $precio1 = $precio1 + $precio2;
    }

}

?>
    
answered by 10.08.2018 / 04:08
source
0

I think this is what you need:

$existencia = 3;
$saldo1 = 1;
$saldo2 = 5;
$total = 0;

$precio1 = 154.16;
$precio2 = 116;

for($i=$saldo1; $i <= $saldo2; $i++){
    if($i > $existencia){
        break;
    }
    $total = ($i==$saldo1) ? $total + $precio1 : $total + $precio2;

}
echo "Total: ". $total;

See DEMO

There we are starting the loop with balance 1 while it is less than or equal to balance 2, Then we validate that if $ i is equal to balance 1, add the price one, otherwise add the price 2 as many times as you subtract to the loop and since we already consume the balance 1, we only have two laps left to equal the existence so we would add 116 twice to the total

    
answered by 10.08.2018 в 10:51