| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 924 人关注过本帖, 2 人收藏
标题:以前有朋友出过的一道题
只看楼主 加入收藏
statics
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:163
专家分:625
注 册:2011-8-29
收藏
得分:14 
用那么多循环?

惟我独行...
2011-09-13 11:15
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
bhu_wll
Rank: 3Rank: 3
来 自:北京
等 级:论坛游侠
帖 子:70
专家分:167
注 册:2011-9-8
收藏
得分:14 
3楼程序时间度有点复杂哦

努力对待生活
2011-09-14 12:25
y1207435881
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:225
专家分:762
注 册:2013-4-30
收藏
得分:0 
高啊
2013-04-30 23:30
快速回复:以前有朋友出过的一道题
数据加载中...
 
   



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

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