目前共有6篇帖子。 內容轉換:不轉換▼
 
點擊 回復
296 5
【程序】C语言用字典排序法列举一个数组的所有全排
一派護法 十九級
1樓 發表于:2015-11-19 23:01
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

void reverse(int *array, const int start, const int length)
{
    int i;
    for (i = 0; i < length / 2; i++)
        swap(&array[start + i], &array[start + length - i - 1]);
}

void reverseAfter(int *array, const int size, const int index)
{
    reverse(array, index + 1, size - index - 1);
}

void printArray(int *array, const int size)
{
    int i;
    for (i = 0; i < size; i++)
    {
        printf("%d", array[i]);
        if (i + 1 == size)
            putchar('\n');
        else
            printf(", ");
    }
}

int nextArrangement(int *array, const int size)
{
    int i = size - 1;
    int j, min, minIndex;
    int flag = 0;
    while (i--)
    {
        if (array[i] < array[i + 1])
        {
            flag = 1;
            break;
        }
    }
    if (flag == 0)
        return 0; // already the last arrangement
        
    min = array[i + 1];
    minIndex = i + 1;
    for (j = i + 2; j < size; j++)
    {
        if (array[j] < min && array[j] > array[i])
        {
            flag = 1;
            min = array[j];
            minIndex = j;
        }
    }
    swap(&array[i], &array[minIndex]);
    reverseAfter(array, size, i);
    return 1;
}

int *cloneArray(const int *array, int size)
{
    size *= sizeof(int);
    int *pArray = (int *)malloc(size);
    memcpy(pArray, array, size);
    return pArray;
}

void showAllArrangements(const int *array, const int size)
{
    int *p1 = cloneArray(array, size);
    do
    {
        printArray(p1, size);
    } while (nextArrangement(p1, size));
    free(p1);
}

int main(int argc, char *argv[])
{
    int list[] = {1, 2, 3, 4, 5};
    showAllArrangements(list, sizeof(list) / sizeof(int));
    return 0;
}
一派護法 十九級
2樓 發表于:2015-11-19 23:01
输出内容:
1, 2, 3, 4, 5
1, 2, 3, 5, 4
1, 2, 4, 3, 5
1, 2, 4, 5, 3
1, 2, 5, 3, 4
1, 2, 5, 4, 3
1, 3, 2, 4, 5
1, 3, 2, 5, 4
1, 3, 4, 2, 5
1, 3,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, 1, 3
4, 2, 5, 3, 1
4, 3, 1, 2, 5
4, 3, 1, 5, 2
4, 3, 2, 1, 5
4, 3, 2, 5, 1
4, 3, 5, 1, 2
4, 3, 5, 2, 1
4, 5, 1, 2, 3
4, 5, 1, 3, 2
4, 5, 2, 1, 3
4, 5, 2, 3, 1
4, 5, 3, 1, 2
4, 5, 3, 2, 1
5, 1, 2, 3, 4
5, 1, 2, 4, 3
5, 1, 3, 2, 4
5, 1, 3, 4, 2
5, 1, 4, 2, 3
5, 1, 4, 3, 2
5, 2, 1, 3, 4
5, 2, 1, 4, 3
5, 2, 3, 1, 4
5, 2, 3, 4, 1
5, 2, 4, 1, 3
5, 2, 4, 3, 1
5, 3, 1, 2, 4
5, 3, 1, 4, 2
5, 3, 2, 1, 4
5, 3, 2, 4, 1
5, 3, 4, 1, 2
5, 3, 4, 2, 1
5, 4, 1, 2, 3
5, 4, 1, 3, 2
5, 4, 2, 1, 3
5, 4, 2, 3, 1
5, 4, 3, 1, 2
5, 4, 3, 2, 1
一派護法 十九級
3樓 發表于:2015-11-19 23:02
一派護法 十九級
4樓 發表于:2015-11-19 23:06
注意,数组中不可以出现重复的数字,否则程序会出错。
一派護法 十九級
5樓 發表于:2015-11-19 23:08
回復:4樓
比如说,当int list[] = {1, 2, 3, 3};
输出的内容是:
1, 2, 3, 3
1, 3, 3, 2
2, 1, 3, 3
2, 3, 3, 1
3, 1, 3, 2
3, 2, 1, 3
3, 2, 3, 1
3, 3, 1, 2
3, 3, 2, 1
其中少了1323, 2313, 3123等排列
一派護法 十九級
6樓 發表于:2016-3-18 09:26
其实,C++标准库里自带了next_permutation函数,生成的顺序就是字典序。
// ConsoleApplication8.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <algorithm>
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    int i;
    int a[] = {1, 2, 3, 4, 5};
    do
    {
        for (i = 0; i < 5; i++)
            cout << a[i] << ' ';
        cout << endl;
    } while (next_permutation(a, a + 5));
    return 0;
}

回復帖子

內容:
用戶名: 您目前是匿名發表
驗證碼:
(快捷鍵:Ctrl+Enter)
 

本帖信息

點擊數:296 回複數:5
評論數: ?
作者: 巨大八爪鱼
最後回復:巨大八爪鱼
最後回復時間:2016-3-18 09:26
 
©2010-2024 Arslanbar Ver2.0
除非另有聲明,本站採用創用CC姓名標示-相同方式分享 3.0 Unported許可協議進行許可。