Skip to content

Instantly share code, notes, and snippets.

@marcoscastro
Last active January 18, 2024 16:44
Show Gist options
  • Save marcoscastro/60f8f82298212e267021 to your computer and use it in GitHub Desktop.
Save marcoscastro/60f8f82298212e267021 to your computer and use it in GitHub Desktop.
C/C++ - Gerando todas as permutações
/* 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;
}
@P81000
Copy link

P81000 commented Aug 27, 2021

Você poderia explicar o funcionamento do seu código? Estou iniciando em programação agora e gostaria de entender como você chegou nesse resultado

@Zooted345
Copy link

thank you man you helped a lot

@Zooted345
Copy link

@ferreira-dev01
Copy link

obrigado! vou reescrever para n threads.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment