Cercul de informatică


                        Program adaptat după manualul de Tehnici de programare pentru liceu:

                     Program în limbajul de programare C++, CodeBlocks, pentru generarea permutărilor de n elemente, unde n este număr natural citit de la tastatură.

Pentru n=3, programul va afișa:

1 2 3

1 3 2

2 1 3

2 3 1

3 1 2

3 2 1




                                      #include <iostream>

                                      using namespace std;

                                      typedef int stiva[100];

                                     int n,k,ev,as;

                                     stiva st;

                                    void init()

                                    {

                                         st[k]=0;

                                    }

                                    int succesor()

                                   {

                                     if(st[k]<n)

                                    {

                                        st[k]=st[k]+1; return 1;

                                     } 

                                  else return 0;


                                }

                                int valid ()

                               {

                                for (int i=1;i<k;i++)

                                     if (st[k]==st[i]) return 0;

                                return 1;

                               }


                            int solutie()

                           {

                                return k==n;

                            }


                            void tipar ()

                            {

                             for (int i=1;i<=n;i++)

                            cout<<st[i]<<" ";

                           cout<<endl;

                            }

                       void bt()

                        {

                             k=1;

                             init();

                            while (k>0)

                           {

                             as=1; ev=0;

                            while (as&&!ev)

                             {

                                 as=succesor();

                                   if(as) ev=valid();

                              }

                            if(as)

                              if (solutie())

                               tipar ();

                             else

                             {

                                     k++;

                                     init();

                             }

                              else

                              k--;

                         }

                      }

                   int main()

                  {

                      cout<<"n="; cin>>n;

                      bt();

                      return 0;

                 }





                   



Comentarii

Postări populare de pe acest blog