Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 我将如何使用递归生成所有可能性?_C_Combinatorics - Fatal编程技术网

C 我将如何使用递归生成所有可能性?

C 我将如何使用递归生成所有可能性?,c,combinatorics,C,Combinatorics,假设输入是4,那么输出应该是所有可能的4个字母的单词,字母a到f。从aaaa一直到ffff。如何通过使用递归来实现这一点 我很抱歉没有在我最初的问题中包括我对这个问题的尝试。有些人想知道为什么我使用递归而不是使用更简单的方法(例如for循环),原因是我的教授希望我们使用for循环来解决这个问题 以下是我的尝试: void allPossiblilities(int n) { char*result; if(Done(result))/*since the last possi

假设输入是4,那么输出应该是所有可能的4个字母的单词,字母a到f。从aaaa一直到ffff。如何通过使用递归来实现这一点

我很抱歉没有在我最初的问题中包括我对这个问题的尝试。有些人想知道为什么我使用递归而不是使用更简单的方法(例如for循环),原因是我的教授希望我们使用for循环来解决这个问题

以下是我的尝试:

void allPossiblilities(int n)
{
     char*result;
     if(Done(result))/*since the last possibility will be all f I am using that as my base case*/
     {
       printf("%s",result);
       return;
     }

 /*This is where the recursive part should go but I am totally lost as to what it should be*/
}

bool Done(result)/*This function just returns true if all the array's index are f*/
{
     int i;
     bool a=true;
     for(i=0;i<=n-1;i++)
        if(result[i]!='f')
           a=false;
}
     return a;
}
void allposabilities(int n)
{
字符*结果;
如果(完成(结果))/*因为最后一种可能性都是f,所以我使用它作为我的基本情况*/
{
printf(“%s”,结果);
返回;
}
/*这是递归部分应该去的地方,但我完全不知道它应该是什么*/
}
bool Done(result)/*如果数组的所有索引都是f,则此函数仅返回true*/
{
int i;
布尔a=真;

对于(i=0;i我将给你一个提示,让你思考:

4位数字和10位可能数字(0-9)的可能性有多少?基数^digits=10^4=10000可能输出0000-9999,在您的情况下,它们将基数=6(A-F)和经验=4(4个位置)6^4=1296组合

递归函数是如何生成的? 它们有两个步骤:

  • 基本步骤:它是函数不调用自身时的条件(最终条件)

  • 递归步骤:它是函数调用自身时的标准或条件,其结果应该更接近基本步骤

例如著名的阶乘函数,基本步骤是返回1,递归步骤是第二步

PD:我想让你自己分析问题,找到解决方案,并给你一些工具

守则:

#include <stdio.h>
#include <stdlib.h>

void recurs( int * s );
void print( int * s );

int main( void )
{
    int a[] = { 0, 0, 0, 0 };
    print( a );
    recurs( a );

}

void recurs( int * s )
{
    int i;

    /*Basic Case*/
    if( s[ 3 ] == 5 && s[ 2 ] == 5 && s[ 1 ] == 5 && s[ 0 ] == 5 ){
        print( s );
        printf( "\nAccomplisshed!\n" );
    }

    else{
        s[ 0 ] += 1;
        for( i = 0; i < 3; i++ ){
            if( s[ i ] == 6 ){
                s[ i ] = 0;
                s[ i + 1 ] += 1;
            }
        }
        print( s );
        recurs( s );
    }
}

/* This only prints. */
void print( int * s )
{
    int i; 
    printf( "    " );
    for( i = 3; i >= 0; i-- ){
        printf( "%c", ( s[ i ] + 65 ) );
    }
}
#包括
#包括
无效递归(int*s);
无效打印(整数*s);
内部主(空)
{
inta[]={0,0,0,0};
印刷品(a);
递归(a);
}
无效递归(int*s)
{
int i;
/*基本情况*/
如果(s[3]==5&&s[2]==5&&s[1]==5&&s[0]==5){
印刷品;
printf(“\n完成!\n”);
}
否则{
s[0]+=1;
对于(i=0;i<3;i++){
如果(s[i]==6){
s[i]=0;
s[i+1]+=1;
}
}
印刷品;
递归;
}
}
/*这只会打印*/
无效打印(整数*s)
{
int i;
printf(“”);
对于(i=3;i>=0;i--){
printf(“%c”,(s[i]+65));
}
}
部分输出:

为了使用十六进制符号生成
a-f
字符:

#include <stdio.h>
int v(unsigned char* i, unsigned short n) {
  return !n || (*i>=0xa0 && (*i&0xf)>=10 && v(i+1,n-1));
}
void f(unsigned short i) {
  if(i) f(i-1);
  if(v((char*)&i,2)) printf("%x\n",i);
}
int main(){ f((1<<16)-1);}
#包括
int v(无符号字符*i,无符号短n){
return!n | |(*i>=0xa0&(*i&0xf)>=10&&v(i+1,n-1));
}
空f(无符号短i){
if(i)f(i-1);
如果(v((char*)&i,2))printf(“%x\n”,i);
}
int main(){f((1
int inc(char*c,char begin,char end){
如果(c[0]==0)返回0;
如果(c[0]==end){//这将使算法停止在char'f'
c[0]=begin;//但是您可以放置任何其他字符
退货公司(c+sizeof(char));
}   
c[0]++;
返回1;
}
int all(int a、int n、字符开始、字符结束){
int i,j;
char*c=malloc((n+1)*sizeof(char));

对于(i=a;iYes,对不起。我认为太晚了,它可能不太清楚。其思想是它在
0
2^16-1
之间的所有整数上循环(
ffff
十六进制),并且仅当它们以十六进制表示法打印时仅包含
f
s或
a
s时才打印它们。无论如何,我的答案对于您的问题非常不合适(即使它有效),我相信你应该关注@alberto bonsanto的提示。谢谢这帮了大忙!但是这个算法适用于大于4甚至小于4的模式吗?
 int inc(char *c,char begin, char end){
    if(c[0]==0) return 0;
    if(c[0] == end){   // This make the algorithm to stop at char 'f'
        c[0]=begin;     // but you can put any other char            
        return inc(c+sizeof(char));
    }   
    c[0]++;
    return 1;
}

int all(int a, int n,char begin, char end){
    int i,j;
    char *c = malloc((n+1)*sizeof(char));
    for(i=a;i<=n;i++){
        for(j=0;j<i;j++) c[j]=begin;
        c[i]=0;
        do {
            printf("%s\n",c);
        } while(inc(c,begin,end));
    }
    free(c);
}


int main(void){
    all(4,4,'a','f'); // Generates from 4 letters words starting in aaaa to ffff
}