Škôlka jazyka C

Fórum o programovaní v jazyku C založené 3. januára 2012

Aktuálny čas je 29 Mar 2024, 10:19

Všetky časy sú v GMT + 1 hodina




Vytvoriť novú tému Odpovedať na tému  [ Príspevok: 1 ] 
Autor Správa
 Predmet príspevku: Permutácie s opakovaním
PoslaťNapísal: 22 Mar 2017, 09:43 
Offline
Stredoškolák
Stredoškolák
Obrázok užívateľa

Registrovaný: 06 Jan 2012, 19:26
Príspevky: 458
Bydlisko: pod Pátrovom
Udelené body: 228 bodov
Získané body: 21 bodov
Kód:
/* C program to print all permutations with repetition of characters*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

/* Following function is used by the library qsort() function
to sort an array of chars */
int compare (const void * a, const void * b);

/* The main function that recursively prints all repeated
permutations of the given string. It uses data[] to store all
permutations one by one */
void allLexicographicRecur (char *str, char* data, int last, int index)
{
   int i, len = strlen(str);

   // One by one fix all characters at the given index and recur for
   // the/ subsequent indexes
   for ( i=0; i<len; i++ )
   {
      // Fix the ith character at index and if this is not the last
      // index then recursively call for higher indexes
      data[index] = str[i] ;

      // If this is the last index then print the string stored in
      // data[]
      if (index == last)
         printf("%s\n", data);
      else // Recur for higher indexes
         allLexicographicRecur (str, data, last, index+1);
   }
}

/* This function sorts input string, allocate memory for data (needed
for allLexicographicRecur()) and calls allLexicographicRecur() for
printing all permutations */
void allLexicographic(char *str)
{
   int len = strlen (str) ;

   // Create a temp array that will be used by allLexicographicRecur()
   char *data = (char *) malloc (sizeof(char) * (len + 1)) ;
   data[len] = '\0';

   // Sort the input string so that we get all output strings in
   // lexicographically sorted order
   qsort(str, len, sizeof(char), compare);

   // Now print all permutaions
   allLexicographicRecur (str, data, len-1, 0);

   // Free data to avoid memory leak
   free(data);
}

// Needed for library function qsort()
int compare (const void * a, const void * b)
{
   return ( *(char *)a - *(char *)b );
}

// Driver program to test above functions
int main()
{
   char str[] = "ABC";
   printf("All permutations with repetition of %s are: \n",
         str);
   allLexicographic(str);
   return 0;
}

_________________
kódy píšem na platforme: linux Ubuntu 12.04 (Geany, Code::Blocks), WinXP (Code::Blocks, PsPad editor),
Skype: libcokamo, ICQ: 56312279
Ak treba, napíš mi na libcosenior@gmail.com. To mám v mobile a stále po ruke.


Detaily príspevku Upozorniť užívateľa
Hore
 Profil  
 
Zobraziť príspevky z predchádzajúceho:  Zoradiť podľa  
Vytvoriť novú tému Odpovedať na tému  [ Príspevok: 1 ] 

Všetky časy sú v GMT + 1 hodina


Kto je on-line

Užívatelia prezerajúci fórum: Žiadny registrovaný užívateľ nie je prítomný a 0 hostia


Rýchla úprava:
Nemôžete zakladať nové témy v tomto fóre
Nemôžete odpovedať na témy v tomto fóre
Nemôžete upravovať svoje príspevky v tomto fóre
Nemôžete mazať svoje príspevky v tomto fóre
Nemôžete zasielať súbory v tomto fóre

Hľadať:
Skočiť na:  
cron
Powered by phpBB® Forum Software © phpBB Group

Hosted by FreeSpace SK