2 jawaban

Jawaban Terbaik
class Param3:

    MAX = 300

    a = 100
    b = 100
    c = 100
    
    def __init__(self, a, b):
        self.a = a
        self.b = b
        self.c = MAX-a-b
        
    def add_A(val):
        # Limitation
        if a+val > MAX:
            val = MAX-a
            print("A > MAX, must be <=")
        elif a+val < 0:
            val = -a
            print("A < 0, must be >=")
        
        a += val
        var coef = b/(b+c)
        b -= val*coef
        c = MAX - a - b
-1
Ixniy 1 Agustus 2020, 01:42

Setiap kali Anda menambah satu variabel, Anda ingin mengurangi dua variabel lainnya. Jika Anda menguranginya masing-masing setengah, jumlah ketiga variabel tetap sama.

Kodesemu:

total = 1; //you could change this to anything

x = total/3;
y = total/3;
z = total/3;

def increaseX(amount) {
    x = x + amount;
    y = y - amount/2;
    z = z - amount/2;
}

def increaseY(amount) {
    y = y + amount;
    x = x - amount/2;
    z = z - amount/2;
}

def increaseZ(amount) {
    z = z + amount;
    x = x - amount/2;
    y = y - amount/2;
}
1
Stefán Erlingur Jónsson 26 Maret 2019, 17:50