数组全排列

解法1:交换

2015某移动音频电台app秋季笔试
第k个元素分别和后面的元素交换,n!
代码如下:

//
//  main.cpp
//  permutation
//
//  Created by LiLingyu on 15/10/21.
//  Copyright © 2015年 LiLingyu. All rights reserved.
//

#include <iostream>

static void swap(int* a, int* b)
{
    int tmp = *a;
    *a = *b;
    *b = tmp;
}

int permutation_core(int* a, int k, int n)
{
    static int count = 0;
    if (k==n-1) {
        //print out
        for (int i=0; i<n; i++) {
            printf("%d\t", a[i]);
        }
        printf("\n");
        count++;
    }
    else{
        for (int i=k; i<n; i++) {
            swap(&a[k], &a[i]);
            permutation_core(a, k+1, n);
            swap(&a[k], &a[i]);
        }

    }
    return count;
}

int main(int argc, const char * argv[]) {
    const int len=5;
    int a[len]={0, 1, 2, 3, 4};

    int n = permutation_core(a, 0, len);

    printf("number: %d\n", n);

    return 0;
}

代码链接:https://github.com/lilingyu/permutation

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在C语言中实现数组全排列,可以使用递归方法。以下是两种不同的实现方法: 方法一: ```c #include <stdio.h> #include <stdlib.h> void swap(int arr[], int a, int b) { int temp = arr[a]; arr[a = arr[b]; arr[b = temp; } void perm(int a[], int begin, int n) { if (begin == n) { for (int i = 0; i <= n; i++) { printf("%d ", a[i]); } printf("\n"); } else { for (int k = begin; k <= n; k++) { swap(a, begin, k); perm(a, begin + 1, n); swap(a, begin, k); } } } int main() { int* a; int n; scanf("%d", &n); a = (int*)calloc(n, sizeof(int)); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } perm(a, 0, n - 1); free(a); return 0; } ``` 方法二: ```c #include <stdio.h> void Permutation(int m, int n, int arr[], int temp[]) { if (m >= n) { for (int i = 0; i < n-1; i++) { printf("%d ", arr[i]); } printf("%d\n", arr[n-1]); } else { for (int i = 0; i < n; i++) { if (temp[i == 0) { temp[i = 1; arr[m = i + 1; Permutation(m + 1, n, arr, temp); temp[i = 0; } } } } int main() { int arr = 0; } Permutation(0, n, arr, temp); return 0; } ``` 以上是两种C语言实现数组全排列的方法,你可以根据需要选择其中的一种方法来使用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [排列组合之——全排列(c语言)](https://blog.csdn.net/m0_74820906/article/details/127779230)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [C语言实现排列组合(全排列)](https://blog.csdn.net/weixin_45652695/article/details/104212424)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值