| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 924 人关注过本帖, 2 人收藏
标题:以前有朋友出过的一道题
取消只看楼主 加入收藏
lz1091914999
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:四川
等 级:贵宾
威 望:37
帖 子:2011
专家分:5959
注 册:2010-11-1
结帖率:94.64%
收藏(2)
已结贴  问题点数:100 回复次数:2 
以前有朋友出过的一道题
A、B、C、D、E、F、G、H、I、J 共10名学生有可能参加本次计算机竞赛,也可能不参加。因为某种原因,他们是否参赛受到下列条件的约束:
   1. 如果A参加,B也参加;
   2. 如果C不参加,D也不参加;
   3. A和C中只能有一个人参加;
   4. B和D中有且仅有一个人参加;
   5. D、E、F、G、H 中至少有2人参加;
   6. C和G或者都参加,或者都不参加;
   7. C、E、G、I中至多只能2人参加   
   8. 如果E参加,那么F和G也都参加。
   9. 如果F参加,G、H就不能参加
   10. 如果I、J都不参加,H必须参加
请编程根据这些条件判断这10名同学中参赛者名单。如果有多种可能,则输出所有的可能情况。每种情况占一行。参赛同学按字母升序排列,用空格分隔。
比如:
C D G J
就是一种可能的情况。
搜索更多相关主题的帖子: 计算机 编程 学生 
2011-09-11 22:39
lz1091914999
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:四川
等 级:贵宾
威 望:37
帖 子:2011
专家分:5959
注 册:2010-11-1
收藏
得分:0 
回复 2楼 beyondyf
大侠你就别来了吧?

My life is brilliant
2011-09-11 23:25
lz1091914999
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:四川
等 级:贵宾
威 望:37
帖 子:2011
专家分:5959
注 册:2010-11-1
收藏
得分:0 
程序代码:
#include <stdio.h>

#define check1(value, index)     ((((value) >> ((index) - 'A')) & 1) == 1)
#define check0(value, index)     ((((value) >> ((index) - 'A')) & 1) == 0)

void print(int test)
{
    int ch;
   
    for (ch = 'A'; ch <= 'J'; ch++)
        if (check1(test, ch))
            printf("%c ", ch);
    printf("\n");
}

int t0(int test) {return check0(test, 'A') || check1(test, 'B');}    /* 如果A参加,B也参加 */
int t1(int test) {return check1(test, 'C') || check0(test, 'D');}    /* 如果C不参加,D也不参加 */
int t2(int test) {return check0(test, 'A') || check0(test, 'C');}    /* A和C中只能有一个人参加 */
int t3(int test) {return (check1(test, 'B') && check0(test, 'D')) || (check0(test, 'B') && check1(test, 'D'));}    /* B和D中有且仅有一个人参加 */
int t4(int test) {return !((check1(test, 'D') && check0(test, 'E') && check0(test, 'F') && check0(test, 'G') && check0(test, 'H')) ||
                           (check0(test, 'D') && check1(test, 'E') && check0(test, 'F') && check0(test, 'G') && check0(test, 'H')) ||
                           (check0(test, 'D') && check0(test, 'E') && check1(test, 'F') && check0(test, 'G') && check0(test, 'H')) ||
                           (check0(test, 'D') && check0(test, 'E') && check0(test, 'F') && check1(test, 'G') && check0(test, 'H')) ||
                           (check0(test, 'D') && check0(test, 'E') && check0(test, 'F') && check0(test, 'G') && check1(test, 'H')) ||
                           (check0(test, 'D') && check0(test, 'E') && check0(test, 'F') && check0(test, 'G') && check0(test, 'H')));}    /* D、E、F、G、H 中至少有2人参加 */
int t5(int test) {return (check1(test, 'C') && check1(test, 'G')) || (check0(test, 'C') && check0(test, 'G'));}    /* C和G或者都参加,或者都不参加 */
int t6(int test) {return !((check1(test, 'C') && check1(test, 'E') && check1(test, 'G') && check1(test, 'I')) ||
                           (check0(test, 'C') && check1(test, 'E') && check1(test, 'G') && check1(test, 'I')) ||
                           (check1(test, 'C') && check0(test, 'E') && check1(test, 'G') && check1(test, 'I')) ||
                           (check1(test, 'C') && check1(test, 'E') && check0(test, 'G') && check1(test, 'I')) ||
                           (check1(test, 'C') && check1(test, 'E') && check1(test, 'G') && check0(test, 'I')));}    /* C、E、G、I中至多只能2人参加 */
int t7(int test) {return check0(test, 'E') || (check1(test, 'F') && check1(test, 'G'));}    /* 如果E参加,那么F和G也都参加 */
int t8(int test) {return check0(test, 'F') || (check0(test, 'G') && check0(test, 'H'));}    /* 如果F参加,G、H就不能参加 */
int t9(int test) {return (check1(test, 'I') || check1(test, 'J')) || check1(test, 'H');}    /* 如果I、J都不参加,H必须参加 */

int main(void)
{
    int i;

    for (i = 1; i <= 0x3FF; i++)
        if (t0(i) && t1(i) && t2(i) && t3(i) && t4(i) && t5(i) && t6(i) && t7(i) && t8(i) && t9(i))
            print(i);

    return 0;
}
图片附件: 游客没有浏览图片的权限,请 登录注册

用位标致来建立解空间,遍历所有情况判断条件输出。


[ 本帖最后由 lz1091914999 于 2011-9-14 09:53 编辑 ]

My life is brilliant
2011-09-14 09:50
快速回复:以前有朋友出过的一道题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015575 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved