Chinaunix首页 | 论坛 | 博客
  • 博客访问: 121994
  • 博文数量: 41
  • 博客积分: 2564
  • 博客等级: 少校
  • 技术积分: 455
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-20 19:17
文章分类

全部博文(41)

文章存档

2009年(41)

我的朋友

分类: C/C++

2009-04-10 18:04:41

“翻转”法:

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

int main()
{
    int n, m;
    char *buf;
    int token, index, cnt, first;
    scanf("%d %d", &n, &m);
    
    buf = (char *)malloc(n + 1);
    memset(buf, 0x00, n + 1 );
    memset(buf, 0x01, m);
    buf[n] = 1;
    
    while (1)
    {
        first = -1;
        token = 0;
        //输出
        for (index=0, cnt=0; index < n; index ++)
        {
            if (buf[index])
            {
                if (first < 0 && !buf[index + 1])
                {
                    first = index; //记录第一个'10'的位置
                    token = cnt; //记录第一个'10'前面1的个数
                }
                cnt ++;
                printf("%d ", index + 1);
                if (cnt == m)
                {
                    break;
                }
            }
        }
        printf("\n");

        //结束
        if(first < 0)
        {
            break;
        }       
        

//翻转并移位
        buf[first] = 0x00;
        buf[first + 1] = 0x01;
        memset(buf, 0x00, first); //将第一个'10'前面的1全部置0
        memset(buf, 0x01, token); //置cnt个1

    }
}

经典递归法:

#include <stdio.h>

void combination(int, int, int, int);

int *buf;
int n, m;

int main()
{    
    while(EOF != scanf("%d", &n))
    {
        scanf("%d", &m);
        buf = (int *)malloc(m * sizeof(int));
        combination(n, n - m + 1, 1, 0);
    }
    return 0;
}

void combination(int n, int max, int begin, int lv)
{
    int index = begin;
    int end = max + 1;
    
    if(max > n)
    {
        for(max = 0; max < m; max ++)
        {
            printf("%d ", buf[max]);
        }
        printf("\n");
        return;
    }
    
    for(; index < end; index ++)
    {
        buf[lv] = index;
        combination(n, max + 1, index + 1, lv + 1);
    }
}


消除递归:

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

void output();

int *buf = NULL;
int m, n;

int main()
{
    int lv, now;
    
    while(EOF != scanf("%d", &n))
    {
        if(!buf)
        {
            free(buf);
        }
        
        scanf("%d", &m);
        buf = (int *)malloc(m * sizeof(int));
        
        lv = 0;
        now = 1;
        buf[lv] = 1;

        while(1)
        {
            if(lv == m - 1)
            {
                while(now <= n)
                {
                    buf[lv] = now;
                    output();
                    now ++;
                }
                lv --;
                now = buf[lv] + 1;
                continue;
            }
            
            buf[lv] = now;
            
            if(now <= n - m + lv + 1)
            {
                now = buf[lv] + 1;
                lv ++;
            }
            else
            {
                if(lv)
                {
                    lv --;
                    now = buf[lv] + 1;
                }
                else
                {
                    break;
                }
            }
        }
    }
}

void output()
{
    int tmp = m;
    
    while(tmp --)
    {
        printf("%d ", buf[tmp]);
    }
    printf("\n");
}

阅读(478) | 评论(0) | 转发(0) |
0

上一篇:malloc/free VS new/delete

下一篇:全排列

给主人留下些什么吧!~~