第1关:命题逻辑公式化简
题目描述
用化简命题逻辑公式的方法设计一个5人表决开关电路,要求3人以上(含3人)同意则表决通过(表决开关亮)。
输入格式:
在一行内输入5人的表决值,1表示通过,0表示不通过,值之间以空格分隔,如:1 0 0 1 1
输出格式:
输出表决器的结果,如:1表示通过,0表示不通过
输入样例:
在这里给出一组输入。例如:
1 0 0 1 1
输出样例:
在这里给出相应的输出。例如: 1
开始你的任务吧,祝你成功!
上答案:
#include<stdio.h>
int vote(int a, int b, int c, int d, int e)
{
//在下面的begin-end之间补充完善代码,实现题目要求
//********begin***************//
int i;
i=a+b+c+d+e;
if(i<3)
return 0;
else
return 1;
}
//*********end*****************//
int main( )
{
int a, b, c, d, e;
scanf("%d%d%d%d%d", &a, &b, &c, &d, &e);
int result = vote(a, b, c, d, e);
printf("%d", result);
}
第2关:命题逻辑推理
题目描述
用命题逻辑推理的方法解决逻辑推理问题。 根据下面的命题,试用逻辑推理方法确定谁是作案者,写出推理过程。 (1)营业员A或B偷了手表; (2)若A作案,则作案不在营业时间; (3)若B提供的证据正确,则货柜末上锁; (4)若B提供的证据不正确,则作案发生在营业时间; (5)货柜上了锁。 符号化上面的命题为: A:营业员A偷了手表 B:营业员B偷了手表 C:作案不在营业时间 D:B提供的证据正确 E:货柜末上锁
输入格式:
本题无输入
输出格式:
A=1,B=0 A=0,B=1 上面第一行表示A偷了手表 第二行表示B偷了手表
输入样例:
在这里给出一组输入。例如:
输出样例:
在这里给出相应的输出。例如: A=1,B=0 A=0,B=1 上面第一行表示A偷了手表 第二行表示B偷了手表
上答案:
#include<stdio.h>
using namespace std;
int main( )
{
//在下面的begin-end之间补充完善代码,实现题目要求
//***************begin***********//
int a[4],num;
for(int i=0;i<=4;i++)
num=a[0]+a[1]+a[2]+a[3];
if(num<=4)
printf("A=1,B=0");
else
printf("A=0,B=1");
//***************end******************//
return 0;
}
第3关:基于规则的分类器
题目描述
机器学习中分类规则模型的表达和应用,将分类模型(规则分类器或决策树分类器)用程序语言表达。实验用例:下面的表数据是动物实际类别数据,即训练样本。
根据此数据,通过机器学习的某种算法产生了包含5条分类规则的规则集: R1: (Give Birth = no) (Can Fly = yes) ⟹ Birds R2: (Give Birth = no) (Live in Water = yes) ⟹ Fishes R3: (Give Birth = yes) (Blood Type = warm) ⟹ Mammals R4: (Give Birth = no) (Can Fly = no) ⟹ Reptiles R5: (Live in Water = sometimes) ⟹ Amphibians 此规则集是有序规则集,即R1>R2>R3>R4>R5。该规则集的使用办法,对未知动物A的分类如下: (1)如果规则前件被A触发,则A被分类为该条规则的后件类别; (2)如果A触发多条规则,则按规则的排序,由先触发的规则后件决定此动物的类别; (3)如果A不能触发任何一条规则,则A被分类为默认类别(Mammals) 。 【实验原理和方法】 (1)将动物用结构体”struct Animal”描述。
struct Animal
{
char name[30];
char bloodType;
char giveBirth;
......
};
注:结构体Animal的属性除name外全部为char类型,其值为上表中值字符串的首字母,也就是,比如: struct Animal eagle; eagle.bloodType = 'w'; eagle.giveBirth='n' (2) 将规则集用函数”void ClassifyAnimal(struct Animal animal)”实现,在函数内直接用printf输出动物的类别。 (3)要求程序判断下面的三个动物类别。
输入格式:
输入依次为:name,bloodType,giveBirth,canFly,liveInWater。以空格分隔
输出格式:
输出该动物的名称和类别。
输入样例:
在这里给出一组输入。例如:
lemur w y n n
输出样例:
在这里给出相应的输出。例如:
lemur is Mammals
上答案:
#include<iostream>
using namespace std;
struct Animal {
char name[30];
char bloodType; // 'w'-woram,'c'-cold
char giveBirth; // 'n'-no,'y'-yes
char canFly; // 'n'-no,'y'-yes
char liveInWater; // 'n'-no,'y'-yes,'s'-somtimes
char type; //'m'-mammals,'r'-reptiles,'f'-fishes,'a'-amphibians,'b'-birds'
};
void classifyAnimal(struct Animal a)
{
//在下面的begin-end之间补充完善代码,实现题目要求
//*******************begin**************************//
if(a.giveBirth=='n'&&a.canFly=='y')
printf("%s is Birds",a.name);
else if(a.giveBirth=='n'&&a.liveInWater=='y')
printf("%s is Fishes",a.name);
else if(a.giveBirth=='y'&&a.bloodType=='w')
printf("%s is Mammals",a.name);
else if(a.giveBirth=='n'&&a.canFly=='n')
printf("%s is Reptiles",a.name);
else if(a.liveInWater=='s')
printf("%s is Amphibians",a.name);
else
printf("%s is Mammals",a.name);
//*******************end****************************//
}
int main( )
{
struct Animal a;
cin >> a.name >> a.bloodType >> a.giveBirth >> a.canFly >> a.liveInWater;
classifyAnimal(a);
return 0;
}