mercredi 20 octobre 2021

split n into different numbers according to the mentioned conditions

input n, a and b. split n into n1, n2 and n3 such that all 3 are either equal to a or b and n=n1+n2+n3. n1≥n2≥n3. print n1,n2,n3 if all conditions are met. if n can't be expressed in this manner print "Cannot be written"

eg: if n=110, a=50, b=30 then n1 = 50, n2 = 50 and n3 = 30 if n=110, a=20, b=50 then "Cannot be written"

where exactly am i going wrong? it shows Cannot be written in every case

n=int(input())
a=int(input())
b=int(input())
n1=int()
n2=int()
n3=int()
if a>b:
    n1==a
    if (n-n1)==2*b:
        n2=b
        n3=b
        if n==(n1+n2+n3):
            print(n1,n2,n3)
    elif (n-n1)==a+b:
        n2==a
        n3==b
        if n==(n1+n2+n3):
            print(n1,n2,n3)
    else:
        print("Cannot be written")
elif a<b:
    n1==b
    if (n-n1)==2*a:
        n2==a
        n3==a
        if n==(n1+n2+n3):
            print(n1,n2,n3)
    elif (n-n1)==a+b:
        n2==b
        n3==a
        if n==n1+n2+n3:
            print(n1,n2,n3)
    else:
        print("Cannot be written")
else:
    print("Cannot be written")

Aucun commentaire:

Enregistrer un commentaire