求集合A与集合B的关系 有什么错误?
来源:学生作业帮 编辑:拍题作业网作业帮 分类:综合作业 时间:2024/05/02 04:19:21
求集合A与集合B的关系 有什么错误?
#include
#include
int IsIncluded ( int *A,int a,int *B,int b);
void main()
{\x05int c;
\x05int *A,i,N;
\x05int *B,H;
\x05printf("please input A length:" );
\x05\x05scanf("%d",&N);
\x05A = (int *) malloc(N * sizeof(int));
\x05for( i=0;i
#include
#include
int IsIncluded ( int *A,int a,int *B,int b);
void main()
{\x05int c;
\x05int *A,i,N;
\x05int *B,H;
\x05printf("please input A length:" );
\x05\x05scanf("%d",&N);
\x05A = (int *) malloc(N * sizeof(int));
\x05for( i=0;i
你的程序没有多大问题,但是你的程序中比较集合的时候是不需要break的,你的问题出在free B 的时候指针越界.我没注意你B的问题
先修改如下:
#include <stdio.h>
#include <stdlib.h>
int IsIncluded ( int *A,int a,int *B, int b);
void main()
{\x05int c;
\x05int *A,i,N;
\x05int *B,H;
\x05printf("please input A length: " );
\x05\x05scanf("%d", &N);
\x05A = (int *) malloc(N * sizeof(int));
\x05for( i=0;i<N;i++)
\x05\x05scanf("%d",&A[i]);
\x05printf("\nplease input B length: " );
\x05\x05scanf("%d", &H);
\x05B = (int *) malloc(N * sizeof(int));
\x05
\x05for( i=0;i<H;i++)
\x05\x05scanf("%d",&B[i]);
\x05c=IsIncluded ( A,N,B,H);
\x05
\x05if (c==1)
\x05\x05printf ( " \n A 包含于 B\n" ) ;
\x05else
\x05\x05printf ( " \n A 不包含于 B\n" ) ;
\x05//free (A);
\x05//free (B);
\x05
}
int IsIncluded ( int *A,int a,int *B, int b)
{
\x05int i,j;
\x05int count =0;
\x05if(a>b)
\x05\x05return 0;
\x05for (i=0;i<a;i++)
\x05{
\x05\x05for (j=0;j<b;j++)
\x05\x05{
\x05\x05\x05if (\x05*(A +i) ==*(B+j))
\x05\x05\x05\x05count++; //break;
\x05\x05\x05
\x05\x05}
\x05}
\x05if (count ==a)
\x05\x05return 1;
\x05else
\x05\x05return 0;
\x05
}
再问: 你能再解释一下 ,free B 为什么越界?? 谢谢,我已经找到自己的错误 ,是因为申请B的空间,应该乘H.
再答: 那现在问题就更明显了: 比如你输入 A长度是 1 数据:1 B长度 2 数据 2 4 B的数据比A的多,但是你申请的长度B是1个,你输入B中的数据实际上是两个,B的数组超过了原来的个数 所以你释放数组B的时候会出错
先修改如下:
#include <stdio.h>
#include <stdlib.h>
int IsIncluded ( int *A,int a,int *B, int b);
void main()
{\x05int c;
\x05int *A,i,N;
\x05int *B,H;
\x05printf("please input A length: " );
\x05\x05scanf("%d", &N);
\x05A = (int *) malloc(N * sizeof(int));
\x05for( i=0;i<N;i++)
\x05\x05scanf("%d",&A[i]);
\x05printf("\nplease input B length: " );
\x05\x05scanf("%d", &H);
\x05B = (int *) malloc(N * sizeof(int));
\x05
\x05for( i=0;i<H;i++)
\x05\x05scanf("%d",&B[i]);
\x05c=IsIncluded ( A,N,B,H);
\x05
\x05if (c==1)
\x05\x05printf ( " \n A 包含于 B\n" ) ;
\x05else
\x05\x05printf ( " \n A 不包含于 B\n" ) ;
\x05//free (A);
\x05//free (B);
\x05
}
int IsIncluded ( int *A,int a,int *B, int b)
{
\x05int i,j;
\x05int count =0;
\x05if(a>b)
\x05\x05return 0;
\x05for (i=0;i<a;i++)
\x05{
\x05\x05for (j=0;j<b;j++)
\x05\x05{
\x05\x05\x05if (\x05*(A +i) ==*(B+j))
\x05\x05\x05\x05count++; //break;
\x05\x05\x05
\x05\x05}
\x05}
\x05if (count ==a)
\x05\x05return 1;
\x05else
\x05\x05return 0;
\x05
}
再问: 你能再解释一下 ,free B 为什么越界?? 谢谢,我已经找到自己的错误 ,是因为申请B的空间,应该乘H.
再答: 那现在问题就更明显了: 比如你输入 A长度是 1 数据:1 B长度 2 数据 2 4 B的数据比A的多,但是你申请的长度B是1个,你输入B中的数据实际上是两个,B的数组超过了原来的个数 所以你释放数组B的时候会出错
集合A与B的差集等于集合B,A与B有什么关系
集合A是集合B的真子集与集合A是集合B的子集之间有什么区别?
我有个问题不懂,题目告诉你集合A在什么范围内 集合B在什么范围内 求集合A和B的关系
集合A属于集合B与集合A含于集合B有什么区别
A∩B与集合A、B、B∩A有什么关系?A∪B与集合A、B、B∪A有什么关系
A,B是两个不同的集合,如A-B=B,那么A与B有什么关系求大神帮助
用适当的符号表示元素a与集合A、b与集合B的关系
原函数定义域A,导函数定义域B 求集合A与B的关系
判断下列集合A与B的关系
写出下列各个集合A与B的关系
集合A中有3,9.集合B中有6,12全集U中有0,3,6,9,12.,15.求集合A与集合B的交集在集合U中的补集
已知集合A中有三个元素1,a+b,a,集合B中有三个元素,0,a分之b,b.若集合A与集合B相等,求ab的