C语言编程入门必做题-编程解决推理题
2006-08-30 17:23
344 查看
2. A、B、C、D、E五名学生有可能参加计算机竞赛,根据下列条件判断哪些
人参加了竞赛:
(1)A参加时,B也参加;
(2)B和C只有一个人参加;
(3)C和D或者都参加,或者都不参加;
(4)D和E中至少有一个人参加;
(5)如果E参加,那么A和D也都参加。
在论坛上看到的,入门者必做题,想起一年前有人问我这个题,我一头雾水,根本不会。现如今,我又看到了,勾起了我的回忆。为什么我那个时候不会呢?真的不知道,其实这个问题很简单的。
判断ABCDE有没有参加,也就是说,ABCDE各有两个取值,要么参加(1),要么不参加(0),然后ABCDE满足5个条件,只要把5个条件表示成计算机能够理解的布尔表达式就可以了,然后对ABCDE的所有指派验证表达式,满足了就停下来,肯定可以求出解。
就是这么个简单问题,我为什么那个时候不会呢?下面是我刚才写的一段C代码,求出的答案是C,D参加了,别人没有参加。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "stdio.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
int main()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int a,b,c,d,e;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (a=0; a<2; a++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(b=0; b<2; b++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(c=0; c<2; c++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(d=0; d<2; d++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(e=0; e<2; e++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(a && !b) continue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if((b && c) || (!b && !c)) continue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(( c && !d) || (!c && d)) continue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (!d && !e) continue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (e && (!a || !d)) continue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("a %d b %d c %d d %d e %d ",a,b,c,d,e);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
人参加了竞赛:
(1)A参加时,B也参加;
(2)B和C只有一个人参加;
(3)C和D或者都参加,或者都不参加;
(4)D和E中至少有一个人参加;
(5)如果E参加,那么A和D也都参加。
在论坛上看到的,入门者必做题,想起一年前有人问我这个题,我一头雾水,根本不会。现如今,我又看到了,勾起了我的回忆。为什么我那个时候不会呢?真的不知道,其实这个问题很简单的。
判断ABCDE有没有参加,也就是说,ABCDE各有两个取值,要么参加(1),要么不参加(0),然后ABCDE满足5个条件,只要把5个条件表示成计算机能够理解的布尔表达式就可以了,然后对ABCDE的所有指派验证表达式,满足了就停下来,肯定可以求出解。
就是这么个简单问题,我为什么那个时候不会呢?下面是我刚才写的一段C代码,求出的答案是C,D参加了,别人没有参加。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "stdio.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
int main()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int a,b,c,d,e;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (a=0; a<2; a++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(b=0; b<2; b++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(c=0; c<2; c++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(d=0; d<2; d++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(e=0; e<2; e++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(a && !b) continue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if((b && c) || (!b && !c)) continue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(( c && !d) || (!c && d)) continue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (!d && !e) continue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (e && (!a || !d)) continue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("a %d b %d c %d d %d e %d ",a,b,c,d,e);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
相关文章推荐
- C语言编程入门必做题-给定等式,猜字母代表的数字
- C语言编程入门必做题-画字符圈圈
- 我的编程生涯的入门语言 - C语言之学员成绩管理
- C语言编程入门——浮点数
- C语言编程入门——程序练习(上)
- C语言编程入门——指针与函数、数组
- Linux上C语言编程入门
- 急需解决C语言的一个画面编程
- 讲解C语言编程中指针赋值的入门实例
- C语言编程入门之程序头文件的简要解析
- c语言是学习编程的一个开始,学习好的c语言,其它语言就容易入门啦!
- 计网_C语言编程获取本机的mac地址以及mac下pcap编程报错Undefined symbols for architecture x86_64-"_pcap_*****"...的解决办法
- python 逻辑推理编程解决八皇后
- PIC单片机入门_C语言编程技术
- C语言编程入门必做题-2~16进制转换
- C语言编程入门——枚举与宏定义
- C语言编程入门——综合练习(二)
- linux操作系统下 c语言编程入门
- Linux上C语言编程入门
- python 示例分享---逻辑推理编程解决八皇后