數組全排列

解法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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章