目前共有6篇帖子。 内容转换:不转换▼
 
点击 回复
318 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, 4,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, 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)
 

本帖信息

点击数:318 回复数:5
评论数: ?
作者:巨大八爪鱼
最后回复:巨大八爪鱼
最后回复时间:2016-3-18 09:26
 
©2010-2024 Arslanbar Ver2.0
除非另有声明,本站采用知识共享署名-相同方式共享 3.0 Unported许可协议进行许可。