C语言编程 集合子集问题 70

本训练中的每类问题都是系列问题,每个问题必须用至少一个子函数来实现集合运算问题设集合A={al,a2,a3…,am},集合B={bl,b2,…,bn},则求下列运算l)判... 本训练中的每类问题都是系列问题,每个问题必须用至少一个子函数来实现

集合运算问题
设集合A={al , a2, a3… ,am },集合B={bl , b2,… ,bn},则求下列运算
l)判断a∈A

2)判断集合B是否为集合A的子集
说明一下,1,,2 ,,,条件是两个不同的程序--。
展开
 我来答
百度网友cd8541ae4
2013-07-02 · TA获得超过264个赞
知道小有建树答主
回答量:126
采纳率:100%
帮助的人:111万
展开全部
#include<iostream> 
#include<string>
#include<fstream>
#include<conio.h>
using namespace std;
//定义集合的最大数量,可以自己修改
#define MAX_COUNT 100    
//集合使用数组来保存
int SetA[MAX_COUNT];
int SetB[MAX_COUNT];
int CountA = 0;   //集合A的长度
int CountB = 0;   //集合B的长度
///函数Contains,判定集合是否包含一个数
///@param set, 集合
///@param count, 集合Set中的元素个数
///@param value, 是否包含的值
///@return set包含value,返回true,否则返回false
bool Contains(int set[], int count, int value)
{
 for(int i = 0; i < count; i++)
  if(value == set[i])
   return true;
 return false;
}
//输入一个集合
void InputSet(int set[], int &count)
{
 string s;
 //读取这一行
 getline(cin, s);
 int i = 0;
 while(i < s.length() )
 {
  int spacePos = 0;
  spacePos = s.find(' ', i);      //查找空格下一个空格的位置
  if(spacePos < 0 || spacePos >= s.length() - 1)    //如果没有空格或者空格超出了字符串范围,跳出循环
   break;
  string temp = s.substr(i, spacePos - i);        
  if(temp == "")                                  //如果字符串为空,执行下一条
   continue; 
  int value = stoi(temp);
  if(!Contains(set, count, value))                //如果集合中已经包含此数字了,就不再添加
   set[count++] = value;
  i = spacePos + 1;
 }
 //读取最后一个数
 string temp = s.substr(i, s.length() - i);
 int value = stoi(temp);
 if(!Contains(set, count, value))
  set[count++] = value;
}
//判断是为setB是否为setA的子集
bool IsSubSet(int setA[], int countA, int setB[], int countB)
{
 for(int i = 0; i < countB; i++)
  if( !Contains(setA, countA, setB[i]))
   return false;
 return true;
}
void main()
{
 cout<<"注意:只能输入整数,并且数字之间以空格隔开。"<<endl<<endl;
 cout<< "请输入集合A:" <<endl;
 InputSet(SetA, CountA);
 cout<< "请输入集合B:" <<endl;
 InputSet(SetB, CountB);
 cout<<endl;
 cout<<"集合A和集合B比较结果"<<endl;
 if(IsSubSet( SetA, CountA, SetB, CountB))
  cout<<"集合B是集合A的子集"<<endl;
 else
  cout<<"集合B不是集合A的子集"<<endl;
 cout<<endl;
 cout<<"按任意键退出"<<endl;
 getch();
}
test_pop
2013-07-02 · TA获得超过376个赞
知道小有建树答主
回答量:332
采纳率:100%
帮助的人:235万
展开全部
#include <stdio.h>

#define A 5        //数组的大小采用宏定义,方便修改
#define B 3
void Contain (int a[], int num);
void Compare (int a[], int b[]);
int main ()
{
 int a[A] = {1, 2, 3, 4, 5};   //数组的元素可以自己设置去验证
 int b[B] = {1, 2, 6};
 int flag = 0;
 
 printf("Input a num:");
 scanf("%d", &flag);
 Contain (a, flag);
 Compare (a, b);
 return 0;
}
/*
a ∈ A ?
*/
void Contain (int a[], int num)
{
 int i;
 for(i=0; i<A; i++)
 {
  if(num == a[i])
  {
   printf("%d ∈ A\n", num);
   break;
  }
 }
// printf("i = %d\n", i);
 if(i == 5)
  printf("%d !∈ A\n", num);
 return;
}
/*判断集合B是否为集合A的子集 */
void Compare (int a[], int b[])
{
 int i, j;

 //如果b数组大小比a数组大
 if(sizeof(b)>sizeof(a))
 {
  printf("B不是A的子集.\n");
  return;
 }
 for(i=0; i<B; i++)
 {
  for(j=0; j<A; j++)
  {
   if(b[i] != a[i])
   {
    printf("B不是A的子集.\n");
    return;
   }
  }
 }
 printf("B是A的子集.\n");
}
追问
1,2条件是两个不同的程序--。
追答

两个main?拆一下就行了我把文件上传给你吧,你看看这两个文件。

 

 

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式