【发布时间】:2014-04-17 23:06:06
【问题描述】:
我已经在这个论坛here 上发布了这个问题,但是看到没有人回答我决定在这里尝试。
基本上,我正在寻找一种方法来置换给定范围内的整数数组,而不会重复排列。过去我很难理解如何找到排列,所以我希望有人可以深入解释我需要实现什么以及为什么。
这是我现在的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define LENGTH 2
double NumberOfPermuationsOfString( int length, char minimum, char maximum )
{
return pow( ( maximum - minimum ) + 1, length );
}
int NextPermutation( char * buffer, char minimum, char maximum )
{
const size_t length = strlen( buffer ) + 1;
int i = 0;
for ( ; i < length; ++i )
{
/* I don't know what to do here... */
}
return 0;
}
void DisplayPermutations( int length, char minimum, char maximum )
{
char buffer[( length + 1 )];
memset( buffer, 0, sizeof( buffer ) );
memset( buffer, minimum, sizeof( buffer ) - 1 );
int i = 0;
do
{
printf( "%s\n", buffer );
} while ( NextPermutation( &buffer[0], minimum, maximum ) );
}
int main( )
{
printf( "Iterating through %0.lf permuations...\n", NumberOfPermuationsOfString( LENGTH, 'a', 'z' ) );
DisplayPermutations( LENGTH, 'a', 'z' );
return 0;
}
这不是 C#,不要让命名约定欺骗了您...
【问题讨论】:
-
“没有重复排列”是什么意思?你打算把 1,2,3 和 3,2,1 一样对待吗?
-
我不想有“abc”一次,然后再出现。这又是怎么重复的?这不是 C#...
-
嗯,C,但我想说的是它适用于 C++。
标签: c++ c arrays string algorithm