Chinaunix首页 | 论坛 | 博客
  • 博客访问: 436491
  • 博文数量: 103
  • 博客积分: 5010
  • 博客等级: 大校
  • 技术积分: 971
  • 用 户 组: 普通用户
  • 注册时间: 2007-06-11 17:22
文章分类
文章存档

2008年(77)

2007年(26)

我的朋友

分类: C/C++

2008-04-24 00:30:24

1.递归解决:

#include <stdio.h>
int s[11] = {1, 3, 0, 5, 3, 5, 6, 8, 8, 2, 12};
int f[11] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
int a[11];

static int select(int s[], int f[], int i, int n);
int main(int argc, char **argv)
{
        int i;

        select(s, f, 0, 10);
        for(i = 0; i < 11; i++){
                if(a[i]){
                        printf("%d ", i+1);
                }
        }
        printf("\n");

        return 0;
}

static int select(int s[], int f[], int i, int n)
{
        int m;

        a[i] = 1;
        m = i + 1;

        while((m <= n) && (s[m] <= f[i])){
                m = m + 1;
        }
        if(m <= n){
                a[m] = 1;
                i = m;
                select(s, f, m, n);
        }

        return 0;
}


2.迭代解决:

#include <stdio.h>

int s[11] = {1, 3, 0, 5, 3, 5, 6, 8, 8, 2, 12};
int f[11] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
int a[11];

int main(int argc, char **argv)
{
        int i, m, len = 11;

        a[0] = 1;
        i = 0;
        for(m = 1; m < 11; m++){
                if(s[m] >= f[i]){
                        a[m] = 1;
                        i = m;
                }
        }

        for(i = 0; i < 11; i++){
                if(a[i]){
                        printf("%d ", i+1);
                }
        }
        printf("\n");

        return 0;
}
~


3.执行结果:
[xxxx@localhost suanfa]$ ./a.out
1 4 8 11

阅读(1041) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~