2016-11-21 4 views
1

Ich versuche, meinen Code die maximale Anzahl an Handys und Zubehör zu kaufen. Ein Telefon kostet 99,99 und ein Zubehör kostet 9,99. Ich habe 1000 auf meinem Bankkonto. Wie kann ich meinen Code den maximalen Betrag kaufen? Mein Code kauft derzeit 9 Telefone und 9 Zubehörteile. Es sollte 9 Telefone und 10 Zubehör kaufen, anstatt 9.Wie man Schleife maximalen Betrag kaufen macht?

const ACCESSORY = 9.99; 
 
const PHONE = 99.99; 
 

 
var balance = 1000; 
 
var total = 0; 
 

 
var phones_total = 0; 
 
var accessories_total = 0; 
 

 
while (((total + PHONE) || (total + ACCESSORY)) < balance) { 
 
    total = total + PHONE; 
 
    phones_total = phones_total + 1; 
 
    if ((total + ACCESSORY) < balance) { 
 
     total = total + ACCESSORY; 
 
     accessories_total = accessories_total + 1; 
 
    } 
 
} 
 

 
console.log("total = " + total); 
 
console.log("phones = " + phones_total); 
 
console.log("accessories = " + accessories_total);

+3

10 Telefone + 10 Zubehör kostet 1099,80, das ist mehr als Sie sich leisten können. – Barmar

+0

Ich denke, du musst dein Problem besser erklären. – Carcigenicate

+0

'while (((gesamt + PHONE) || (gesamt + ZUBEHÖR)) Barmar

Antwort

2

Diese Zeile ist falsch:

while (((total + PHONE) || (total + ACCESSORY)) < balance) { 

Dies gilt nicht prüfen, ob entweder Summe kleiner als balance. Der Wert (total + PHONE) || (total + ACCESSORY) ist immer total + PHONE, es sei denn 0, dann ist es total + ACCESSORY. Und da total + PHONE nie 0 ist, dann ist dies effektiv äquivalent zu:

while ((total + PHONE) < balance) { 

und testet es nie, ob es nur für Zubehör erhältlich Balance ist. Der richtige Weg, um diesen Test zu tun ist:

while ((total + PHONE) < balance || (total + ACCESSORY) < balance) { 

Sie sollten auch <= verwenden werden, anstatt <, für die Verwendung Ihr ganzes Geld genau zu ermöglichen.

Aber dann innerhalb der Schleife, fügen Sie immer noch PHONE zu total hinzu, auch wenn nur noch genügend Restbetrag für ein Zubehörteil übrig war. Sie müssen zuerst überprüfen.

const ACCESSORY = 9.99; 
 
const PHONE = 99.99; 
 

 
var balance = 1000; 
 
var total = 0; 
 

 
var phones_total = 0; 
 
var accessories_total = 0; 
 

 
while ((total + PHONE) <= balance || (total + ACCESSORY) <= balance) { 
 
    if ((total + PHONE) <= balance) { 
 
    total = total + PHONE; 
 
    phones_total = phones_total + 1; 
 
    } 
 
    if ((total + ACCESSORY) <= balance) { 
 
    total = total + ACCESSORY; 
 
    accessories_total = accessories_total + 1; 
 
    } 
 
} 
 

 
console.log(phones_total, accessories_total);

Ein einfacher Weg, es zu tun ist, mit Arithmetik statt einer Schleife. Teilen Sie das Guthaben durch die Kosten für ein Telefon + Zubehör, um herauszufinden, wie viele Paare Sie sich leisten können. Dann finden Sie heraus, wie viele weitere Zubehörteile Sie kaufen können, mit dem, was danach übrig ist.

const ACCESSORY = 9.99; 
 
const PHONE = 99.99; 
 

 
var balance = 1000; 
 

 
var phones_total = Math.floor(balance/(PHONE + ACCESSORY)); 
 
var remainder = balance - phones_total*(PHONE+ACCESSORY) 
 
var accessories_total = phones_total + Math.floor(remainder/ACCESSORY); 
 

 
console.log(phones_total, accessories_total);

1

Hier gehen Sie:

const ACCESSORY = 9.99; 
const PHONE = 99.99; 

var balance = 1000; 
var total = 0; 

var phones_total = 0; 
var accessories_total = 0; 

while (((total + PHONE) < balance) || ((total + ACCESSORY) < balance)) { 
    if((total + PHONE) < balance){ 
     total = total + PHONE; 
     phones_total = phones_total + 1; 
    } 
    if ((total + ACCESSORY) < balance) { 
     total = total + ACCESSORY; 
     accessories_total = accessories_total + 1; 
    } 
} 

console.log("total = " + total); 
console.log("phones = " + phones_total); 
console.log("accessories = " + accessories_total); 

Aber mit dieser Balance können Sie nur 9 Telefone und 10 Zubehör kaufen.

1

Im Schleifenzustand müssen Sie nur prüfen, ob für ein Zubehörteil noch Geld übrig ist. Wenn Sie dies nicht tun, müssen Sie den Körper nicht ausführen.

while ((total + ACCESSORY) <= balance) { 
    if ((total + PHONE) <= balance) { 
     ++ phones_total; 
     total = (phones_total * PHONE) + (accessories_total * ACCESSORY); 
    } 

    if ((total + ACCESSORY) <= balance) { 
     ++ accessories_total; 
     total = (phones_total * PHONE) + (accessories_total * ACCESSORY); 
    } 
} 

Im Körper ist zunächst zu überprüfen, ob es Geld für ein Telefon ist, wenn ja Summen aktualisieren, dann sehen, wenn Sie immer noch Geld für ein Zubehör haben.

Bei Gleitkommazahlen führt das wiederholte Hinzufügen dieser Werte zu einem kumulativeren Fehler als die einfache Berechnung des total wie oben.