穷举排列组合算法

//

//  main.m

//  test

//

//  Created by mac on 16-3-1.

//  Copyright (c) 2016年 _MyCompany_. All rights reserved.

//

 

#import <Foundation/Foundation.h>

 

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

 

static const char alphabet[] =

"abcdefghijklmnopqrstuvwxyz"

"ABCDEFGHIJKLMNOPQRSTUVWXYZ"

"0123456789";

 

static const int alphabetSize = sizeof(alphabet) - 1;

 

void bruteImpl(char* str, int index, int maxDepth)

{

    for (int i = 0; i < alphabetSize; ++i)

    {

        str[index] = alphabet[i];

        

        if (index == maxDepth - 1) printf("%s\n", str);

        else bruteImpl(str, index + 1, maxDepth);

    }

}

 

void bruteSequential(int maxLen)

{

    char* buf = malloc(maxLen + 1);

    

    for (int i = 1; i <= maxLen; ++i)

    {

        memset(buf, 0, maxLen + 1);

        bruteImpl(buf, 0, i);

    }

    

    free(buf);

}

 

static const int BUFFLEN=1024*100; void brute2(int maxLen) { char* indices = malloc(maxLen + 1); char* terminal = indices+maxLen; char *printbuff = malloc(BUFFLEN); char *pbend = &printbuff[BUFFLEN-1]; char *b = printbuff; *pbend = '\0'; ++indices[0]; char *p; while (*terminal == 0) { // print value for (p = indices; *p; ++p) ; for (--p ; p >= indices; --p) { *b++ = alphabet[*p-1]; if (b == pbend) { fwrite(printbuff, 1, b-printbuff, stdout); b = printbuff; } } *b++ = '\n'; if (b == pbend) { fwrite(printbuff, 1, b-printbuff, stdout); b = printbuff; } // increment values int carry = 1; for (++p ; carry; ++p) 

转载于:https://www.cnblogs.com/PJXWang/p/5231464.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值