permutations

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
/*
Below are the permutations of string ABC.
ABC ACB BAC BCA CBA CAB
ABSG
ABGS ABSG AGBS AGSB ASBG ASGB
BAGS BASG BGAS BGSA BSAG BSGA 
GABS GASB GBAS GBSA GSAB GSBA
SABG SAGB SBAG SBGA SGAB SGBA

*/

char *input;
int count;
void getInput(){
    input = calloc(32, sizeof(char));
    scanf("%s", input);
    count = strlen(input);
}


void perumtations2(int n){
    if(n == count - 1){
        return ;
    }
        
    char m = input[n];
    input[n] = input[n + 1];
    input[n + 1] = m;
    
    printf("%s ", input);
    
    perumtations2(n + 1);
    
}
int calculator = 0;
void permutations(int n){
    if(n == count){
        calculator++;
        printf("%s ", input);
        return ;
    }
    for(int i = n; i < count; i++){
        char m = input[i];
        input[i] = input[n];
        input[n] = m;
        
        permutations(n + 1);
        
        m = input[i];
        input[i] = input[n];
        input[n] = m;
    }
    
    return false;
}

int main(){
    getInput();
    printf("The solution is:\n");
    permutations(0);
    printf("\n");
    printf("calculator:%d\n",calculator);
    free(input);
    
    return 0; 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值