Last active
January 18, 2024 16:44
-
-
Save marcoscastro/60f8f82298212e267021 to your computer and use it in GitHub Desktop.
C/C++ - Gerando todas as permutações
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Gera todas as permutações */ | |
#include <stdio.h> | |
void troca(int vetor[], int i, int j) | |
{ | |
int aux = vetor[i]; | |
vetor[i] = vetor[j]; | |
vetor[j] = aux; | |
} | |
void permuta(int vetor[], int inf, int sup) | |
{ | |
if(inf == sup) | |
{ | |
for(int i = 0; i <= sup; i++) | |
printf("%d ", vetor[i]); | |
printf("\n"); | |
} | |
else | |
{ | |
for(int i = inf; i <= sup; i++) | |
{ | |
troca(vetor, inf, i); | |
permuta(vetor, inf + 1, sup); | |
troca(vetor, inf, i); // backtracking | |
} | |
} | |
} | |
int main(int argc, char *argv[]) | |
{ | |
int v[] = {1, 2, 3, 4}; | |
int tam_v = sizeof(v) / sizeof(int); | |
permuta(v, 0, tam_v - 1); | |
return 0; | |
} |
thank you man you helped a lot
the explanation bellow:
obrigado! vou reescrever para n threads.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Você poderia explicar o funcionamento do seu código? Estou iniciando em programação agora e gostaria de entender como você chegou nesse resultado