二元关系c语言编码,[求助]判断一个二元关系是不是自反的和对称的

平时很少做离散题,练练

[CODE]

#include

#include

#include

int locate(int *element,int len,int value)

{

for(int i=0;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

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

{

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

if(i

isReflexive=false;

for(i=0;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]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值