平时很少做离散题,练练
[CODE]
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int locate(int *element,int len,int value)
{
for(int i=0;i<len;i++)
if(element[i]==value)
return i;
return -1;
}
int main()
{
int i,n,m,x,y,px,py;
int *element=NULL;
bool *relation=NULL;
bool isReflexive=true;
bool isSymmetic=true;
printf("Please enter the number of the elements of the domain set:\n");
scanf("%d",&n);
element=(int *)malloc(sizeof(int)*n);//添加检查内存申请是否成功
printf("Please enter the elements of the domain set:\n");
for(i=0;i<n;i++)
scanf("%d",element+i);
relation=(bool *)malloc(sizeof(bool)*n*n);//添加检查内存申请是否成功
memset(relation,false,sizeof(bool)*n*n);
printf("Please enter the number of the elements of the relation R:\n");
scanf("%d",&m);
printf("Please enter the elements of the relation R:\n");
for(i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
px=locate(element,n,x);
py=locate(element,n,y);
if(px>=0&&py>=0)
relation[px*n+py]=true;
else
{
printf("Wrong Input!\n");
exit(1);
}
}
for(i=0;i<n&&relation[i*n+i]==true;i++);
if(i<n)
isReflexive=false;
for(i=0;i<n*n;i++)
{
px=i/n;
py=i%n;
if(relation[i]&&relation[py*n+px]||!relation[i]&&!relation[py*n+px]);//判断同或
else
{
isSymmetic=false;
break;
}
}
if(isReflexive)
printf("The Relation R is reflexive.\n");
else printf("The Relation R is not reflexive.\n");
if(isSymmetic)
printf("The Relation R is symmetic.\n");
else printf("The Relation R is not symmetic.\n");
return 0;
}
[/CODE]