Jump to content

[Python] Generare Combinari


5061756C
 Share

Recommended Posts

st=[0,0,0,0,0,0,0,0,0,0,0,0,0,0]
n=0
c=0
def valid(k):
    if k>1 and st[k]>= st[k-1]:
            return 0
    return 1
        
def tipar(k):
    aux=""
    for i in range(1,k+1):
        aux=aux+str(st[i])
    print(aux)
 
def back(k):
    for i in range(1,n+1):
        st[k]=i
        if valid(k) == 1:
            if k==c:
                tipar(k)
            else:
                back(k+1)
 
n=int(input("Vrei sa faci combinari de : "))
c=int(input("Vrei sa faci combinari de "+str(n) +" luate cate: "))
back(1)
input("")
Pentru combinari de 6 luate cate 4 avem output:

Vrei sa faci combinari de : 6
Vrei sa faci combinari de 6 luate cate: 4
4321
5321
5421
5431
5432
6321
6421
6431
6432
6521
6531
6532
6541
6542
6543
Edited by RW Paul
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.