Kombinátor

Programovacie jazyky, rady, poradňa...
Biohazard66
Light Expert
Light Expert
Používateľov profilový obrázok
Príspevky: 63
Registrovaný: 08 nov 2011, 19:16

Kombinátor

Príspevok od používateľa Biohazard66 »

Mám kombinátor čo kombinuje čísla a písmena ale veľmi ma zaujala jedna vec:
Keď do programu zadám napr. 1234 tak dostanem 24 možností čo je správne ale keď zadám 4321 dostanem len jednu možnosť a tak isto aj so Stringom. Ak chcem dostať správny výsledok musím pri číslach ich zadať v postupnosti a pri Stringu v abecednom poradí a tak sa pýtam prečo to tak je a dá sa to opraviť?
Cyklus kombinácie:

Kód: Vybrať všetko

QString *xs= new QString(ui->lineEdit->text());


    do
    {
    ui->label->setText(ui->label->text()+ *xs + " " );
    c++;
    }
    while (std::next_permutation(xs->begin(),xs->end() ));
    ui->label_2->setText(QVariant(c).toString() + " možností");
metthal
Guru wannabe
Guru wannabe
Používateľov profilový obrázok
Príspevky: 2475
Registrovaný: 26 jan 2006, 18:32
Bydlisko: Nitra / Brno

Re: Kombinátor

Príspevok od používateľa metthal »

Citacia z http://www.cplusplus.com/reference/algo ... rmutation/
Rearranges the elements in the range [first, last) into the lexicographically next greater permutation of elements. The comparisons of individual elements are performed using either operator< for the first version, or comp for the second.
Pokial teda chces pouzivat next_permutation, musis to usporiadat od najmensieho po najvacsi. Ak ten vstup ziskavas od uzivatela, tak to predtym prezen nejakym sortom a bude to fungovat ako ma.
Viper
Medium Expert
Medium Expert
Používateľov profilový obrázok
Príspevky: 137
Registrovaný: 14 mar 2012, 13:19

Re: Kombinátor

Príspevok od používateľa Viper »

Presne ako povedal wolf uprav to takto:

Kód: Vybrať všetko

    QString *xs= new QString(ui->lineEdit->text());
    qSort(xs->begin(),xs->end());

    do
    {
    ui->label->setText(ui->label->text()+ *xs + " " );
    c++;
    }
    while (std::next_permutation(xs->begin(),xs->end() ));
Napísať odpoveď