一道C语言题,米有思路啊。

Peugeot_Heart 2012-08-09 06:41:06
A、B、C、D、E五名学生有可能参加计算机竞赛,根据下列条件判断哪些
人参加了竞赛:
(1)A参加时,B也参加;
(2)B和C只有一个人参加;
(3)C和D或者都参加,或者都不参加;
(4)D和E中至少有一个人参加;
(5)如果E参加,那么A和D也都参加。

编写C代码求解
...全文
488 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
未央宫主 2012-08-11
  • 打赏
  • 举报
回复

#define MASK(v,p) (((1 << p) & v) >> p)

int Enum()
{
int i = 0;
int j = 0;
int nFlag = 0;
for (i = 0; i < 32; i++)
{
if (MASK(i, 0))
{
if (!MASK(i, 1))
{
continue;
}
}
if (!(MASK(i, 1) ^ MASK(i, 2)))
{
continue;
}
if (MASK(i, 2) ^ MASK(i, 3))
{
continue;
}
if (!MASK(i, 3) && !MASK(i, 4))
{
continue;
}
if (MASK(i, 4) &&(!MASK(i, 0) || !MASK(i, 3)))
{
continue;
}
nFlag = 1;
break;
}
if (nFlag == 1)
{
for (j = 0; j < 5; j++)
{
char c = 'A' + j;
if (MASK(i, j))
{
printf("%c 参加\n", c);
}
else
{
printf("%c 不参加\n", c);
}
}
return i;
}
return -1;
}
mnxm 2012-08-10
  • 打赏
  • 举报
回复
for( i = 0 ; i < 32 ; i++ ) //i = 2^5
{
if( i % 2 == 1 && ( i / 2 ) % 2 == 0 ) // A参加时,B也参加
continue;
if( ( i / 2 ) % 2 == 1 && ( i / 4 ) % 2 == 1 ) //B和C只有一个人参加
continue;
if( ( i / 2 ) % 2 == 0 && ( i / 4 ) % 2 == 0 ) //B和C只有一个人参加
continue;
if( ( i / 4 ) % 2 == 1 && ( i / 8 ) % 2 == 0 ) //C和D或者都参加,或者都不参加
continue;
if( ( i / 4 ) % 2 == 0 && ( i / 8 ) % 2 == 1 ) //C和D或者都参加,或者都不参加
continue;
if( ( i / 8 ) % 2 == 0 && ( i / 16 ) % 2 == 0 ) //D和E中至少有一个人参加
continue;
if( ( i / 16 ) % 2 == 1 )
{
if( i % 2 == 0 ) //如果E参加,那么A和D也都参加
continue;
if( i % 8 == 0 ) //如果E参加,那么A和D也都参加
continue;
}
printf("A:%d B:%d C:%d D:%d E:%d \n", i % 2 , ( i / 2 ) % 2 , ( i / 4 ) % 2 , ( i / 8 ) % 2 , ( i / 16 ) % 2 ); //打印 1:Y 0:N
}
mnxm 2012-08-10
  • 打赏
  • 举报
回复
哦 错了 break 改成 continue
mnxm 2012-08-10
  • 打赏
  • 举报
回复
for( i = 0 ; i < 32 ; i++ ) //i = 2^5
{
if( i % 2 == 1 && ( i / 2 ) % 2 == 0 )
break ;
if( ( i / 2 ) % 2 == 1 && ( i / 4 ) % 2 == 1 )
break ;
if( ( i / 2 ) % 2 == 0 && ( i / 4 ) % 2 == 0 )
break ;
if( ( i / 4 ) % 2 == 1 && ( i / 8 ) % 2 == 0 )
break ;
if( ( i / 4 ) % 2 == 0 && ( i / 8 ) % 2 == 1 )
break ;
if( ( i / 8 ) % 2 == 0 && ( i / 16 ) % 2 == 0 )
break ;
if( ( i / 16 ) % 2 == 1 )
{
if( i % 2 == 0 )
break ;
if( i % 8 == 0 )
break ;
}
printf("A:%d B:%d C:%d D:%d E:%d ", i % 2 , ( i / 2 ) % 2 , ( i / 4 ) % 2 , ( i / 8 ) % 2 , ( i / 16 ) % 2 ); //1:Y 0:N
}
xwq4fen3 2012-08-10
  • 打赏
  • 举报
回复

for (int i = 0; i < (1 << 5); ++i)
{
if (((i & 0x01) != 0) != ((i & 0x02) != 0)) continue;
if (((i & 0x02) != 0) == ((i & 0x04) != 0)) continue;
if (((i & 0x04) != 0) != ((i & 0x08) != 0)) continue;
if (((i & 0x08) == 0) && ((i & 0x10) == 0)) continue;
if (((i & 0x10) != 0) && (((i & 0x01) == 0) || ((i & 0x08) == 0))) continue;

printf("A%s,B%s,C%s,D%s,E%s。\n",
i & 0x01 ? "参加" : "不参加",
i & 0x02 ? "参加" : "不参加",
i & 0x04 ? "参加" : "不参加",
i & 0x08 ? "参加" : "不参加",
i & 0x10 ? "参加" : "不参加");
}
xwq4fen3 2012-08-10
  • 打赏
  • 举报
回复

for (int i = 0; i < (1 << 5); ++i)
{
if (((i & 0x01) != 0) != ((i & 0x02) != 0)) continue;
if (((i & 0x02) != 0) == ((i & 0x04) != 0)) continue;
if (((i & 0x04) != 0) != ((i & 0x08) != 0)) continue;
if (((i & 0x08) == 0) && ((i & 0x10) == 0)) continue;
if (((i & 0x10) != 0) && (((i & 0x01) == 0) || ((i & 0x08) == 0))) continue;

printf("A%s,B%s,C%s,D%s,E%s。\n",
i & 0x01 ? "参加" : "不参加",
i & 0x02 ? "参加" : "不参加",
i & 0x04 ? "参加" : "不参加",
i & 0x08 ? "参加" : "不参加",
i & 0x10 ? "参加" : "不参加");
}
xwq4fen3 2012-08-10
  • 打赏
  • 举报
回复

for (int i = 0; i < (1 << 5); ++i)
{
if (((i & 0x01) != 0) != ((i & 0x02) != 0)) continue;
if (((i & 0x02) != 0) == ((i & 0x04) != 0)) continue;
if (((i & 0x04) != 0) != ((i & 0x08) != 0)) continue;
if (((i & 0x08) == 0) && ((i & 0x10) == 0)) continue;
if (((i & 0x10) != 0) && (((i & 0x01) == 0) || ((i & 0x08) == 0))) continue;

printf("A%s,B%s,C%s,D%s,E%s。\n",
i & 0x01 ? "参加" : "不参加",
i & 0x02 ? "参加" : "不参加",
i & 0x04 ? "参加" : "不参加",
i & 0x08 ? "参加" : "不参加",
i & 0x10 ? "参加" : "不参加");
}
  • 打赏
  • 举报
回复
大家加油
titer1 2012-08-09
  • 打赏
  • 举报
回复
怎么看起来 像作业题啊,
大家在暑假都努力啊
goldbeef 2012-08-09
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;

bool condition1(int state)
{
if (((state&(4<<1))>>4))
{
if (((state&(3<<1))>>3))
{
return true;
}
else
{
return false;
}
}
else
{
return true;
}

}

bool condition2(int state)
{
return (((state&(3<<1))>>3)+((state&(2<<1))>>2))==1;
}

bool condition3(int state)
{
return ((((state&(2<<1))>>2)+((state&(1<<1))>>1))==0 ||(((state&(2<<1))>>2)+((state&(1<<1))>>1))==2);
}

bool condition4(int state)
{
return (((state&(1<<1))>>1)+(state&1))>=1;
}

bool condition5(int state)
{
if (state&1)
{
if (((state&(4<<1))>>4)+((state&(1<<1))>>1)==2)
{
return true;
}
else
{
return false;
}
}
else
{
return true;
}
}

int main(void)
{
int state;
char pl[]="ABCDE";
int i;
for (state=0;state<32;state++)
{
if (condition1(state)&&condition2(state)&&condition3(state)&&condition4(state)&&condition5(state))
{
for (i=4;i>=0;i--)
{
if (state&(i<<1))
{
cout<<pl[4-i]<<" ";
}
}
cout<<endl;
}
}

return 0;
}
______CQ_ 2012-08-09
  • 打赏
  • 举报
回复
14的程序有问题。。这儿重发


#include <stdio.h>
#include <conio.h>
//参见记为1,
//没参加记为0;

void main()
{
bool func(int a,int b,int c,int d,int e);
int A,B,C,D,E;
for(int a1=0;a1<=1;a1++)
{
A=a1;
for(int a2=0;a2<=1;a2++)
{
B=a2;
for(int a3=0;a3<=1;a3++)
{
C=a3;
for(int a4=0;a4<=1;a4++)
{
D=a4;
for(int a5=0;a5<=1;a5++)
{
E=a5;
if(func(A,B,C,D,E))
printf("%3d%3d%3d%3d%3d",A,B,C,D,E);
}
}
}
}

}
getch();
}

bool func(int a,int b,int c,int d,int e)
{
int a1,b1,c1,d1,e1;
if(a)
{
if(!b)
{
return 0;
}
a1=1;
}

if(!(b&&c))
{
if(!(b||c))
{
return 0;
}
b1=1;
}
else
{
return 0;
}

if(!(c&&d))
{
if((c||d))
{
return 0;
}
c1=1;
}

if(!(d||e))
{
return 0;
}
else
{
d1=1;
}

if(e)
{
if(!(a&&d))
{
return 0;
}
e1=1;
}

if(a1&&b1&&c1&&d1&&e1)
return 1;
else
return 0;
}
______CQ_ 2012-08-09
  • 打赏
  • 举报
回复
用0表示参加。用1表示不参加

c程序如下。不过有些乱。。


#include <stdio.h>
#include <conio.h> //这个头是后面的getch用的
//参加记为1,
//没参加记为0;

void main()
{
bool func(int a,int b,int c,int d,int e);
int A,B,C,D,E;
for(int a1=0;a1<=1;a1++)
{
A=a1;
for(int a2=0;a2<=1;a2++)
{
B=a2;
for(int a3=0;a3<=1;a3++)
{
C=a3;
for(int a4=0;a4<=1;a4++)
{
D=a4;
for(int a5=0;a5<=1;a5++)
{
E=a5;
if(func(A,B,C,D,E))
printf("%3d%3d%3d%3d%3d",A,B,C,D,E);
}
}
}
}

}
getch();
}

bool func(int a,int b,int c,int d,int e)
{
int a1,b1,c1,d1,e1;
if(a)
{
if(!b)
{
return 0;
}
a1=1;
}

if(!(b&&c))
{
if(!(b||c))
{
return 0;
}
b1=1;
}
else
{
return 0;
}

if(!(c&&d))
{
if((c||d))
{
return 0;
}
c1=1;
}

if(!(d||e))
{
return 0;
}
else
{
d1=1;
}

if(e)
{
if(!(a&&d))
{
return 0;
}
e1=1;
}

if(a1&&b1&&c1&&d1&&e1)
return 1;
else
return 0;
}
y799201384 2012-08-09
  • 打赏
  • 举报
回复
假如E参加能推出矛盾
冰与火 2012-08-09
  • 打赏
  • 举报
回复
就是这个意思[Quote=引用 11 楼 的回复:]

是不是列出所有的情况,让后找出符合要求的组合?引用 9 楼 的回复:

循环枚举嘛,就是列好逻辑式,然后让每个变量枚举所有可能的值。逻辑式你会变吗?把每个人的关系变成真真假假的关系引用 5 楼 的回复:

思路是怎样的喃引用 3 楼 的回复:

C/C++ code
int a = 0 , b = 0 , c = 0 , d = 0 , e = 0 ;
while(a < ……
[/Quote]
Peugeot_Heart 2012-08-09
  • 打赏
  • 举报
回复
是不是列出所有的情况,让后找出符合要求的组合?[Quote=引用 9 楼 的回复:]

循环枚举嘛,就是列好逻辑式,然后让每个变量枚举所有可能的值。逻辑式你会变吗?把每个人的关系变成真真假假的关系引用 5 楼 的回复:

思路是怎样的喃引用 3 楼 的回复:

C/C++ code
int a = 0 , b = 0 , c = 0 , d = 0 , e = 0 ;
while(a < 2 )
{
b = 0;
while ( b<2)
{
c= 0;
……
[/Quote]
冰与火 2012-08-09
  • 打赏
  • 举报
回复
在位上,a是第5位,而是第1位。所有可能的取值就是00000~11111(二进制),就是0到31嘛。[Quote=引用 8 楼 的回复:]

为什么要遍历0-31啊。求详解。引用 7 楼 的回复:

C/C++ code
char text = 0;
while ( text < 32 )
{
char n =bool (text &amp; 0x8) +bool(text &amp;0x4);
if ( n>0 &amp;&amp; n <2)
{
if ( bool(text &……
[/Quote]
冰与火 2012-08-09
  • 打赏
  • 举报
回复
循环枚举嘛,就是列好逻辑式,然后让每个变量枚举所有可能的值。逻辑式你会变吗?把每个人的关系变成真真假假的关系[Quote=引用 5 楼 的回复:]

思路是怎样的喃引用 3 楼 的回复:

C/C++ code
int a = 0 , b = 0 , c = 0 , d = 0 , e = 0 ;
while(a < 2 )
{
b = 0;
while ( b<2)
{
c= 0;
d = 0 ;
while( c<2)
……
[/Quote]
Peugeot_Heart 2012-08-09
  • 打赏
  • 举报
回复
为什么要遍历0-31啊。求详解。[Quote=引用 7 楼 的回复:]

C/C++ code
char text = 0;
while ( text < 32 )
{
char n =bool (text & 0x8) +bool(text &0x4);
if ( n>0 && n <2)
{
if ( bool(text & 0……
[/Quote]
冰与火 2012-08-09
  • 打赏
  • 举报
回复
	char text = 0;
while ( text < 32 )
{
char n =bool (text & 0x8) +bool(text &0x4);
if ( n>0 && n <2)
{
if ( bool(text & 0x4) == bool(text & 0x2))
{
if ((text&0x2)||(text&0x1))
{
if (1== text &0x1)
{
if (1 == bool(text&0x10) ==bool( text&0x2))
break;
}
else
break;
}
}
}
text++;
}
那个又错了,郁闷[Quote=引用 6 楼 的回复:]

根据这位大哥思路:引用 2 楼 的回复:

定义unsigned char型变量i
规定二进制下0-4位是分别代表五名学生,0代表不参加,1代表参加
然后从0至31遍历i判断

C/C++ code
char text = 0;
while ( text < 32 )
{
char n =bool (text & 0x8) +boo……
[/Quote]
冰与火 2012-08-09
  • 打赏
  • 举报
回复
根据这位大哥思路:[Quote=引用 2 楼 的回复:]

定义unsigned char型变量i
规定二进制下0-4位是分别代表五名学生,0代表不参加,1代表参加
然后从0至31遍历i判断
[/Quote]
	char text = 0;
while ( text < 32 )
{
char n =bool (text & 0x8) +bool(text &0x4);
if ( n>0 && n <2)
{
if ( bool(text & 0x4) == bool(text & 0x2))
{
if ((text&0x2)||(text&0x1))
{
if (1== text &0x1)
{
if (1== text&0x10 == text&0x2)
{
if (text &0x10 ==1 )
{
if ( text&0x8 ==1)
{
break;
}
}
else
break;
}
}
else
break;
}
}
}
text++;
}
加载更多回复(5)

69,364

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧