给定两个非空集合A和B,集合的元素为30000以内的正整数,编写程序求A-B。
输入格式:
输入为三行。第1行为两个整数n和m,分别为集合A和B包含的元素个数,1≤n, m ≤10000。第2行表示集合A,为n个空格间隔的正整数,每个正整数不超过30000。第3行表示集合B,为m个空格间隔的正整数,每个正整数不超过30000。
输出格式:
输出为一行整数,表示A-B,每个整数后一个空格,各元素按递增顺序输出。若A-B为空集,则输出0,0后无空格。
输入样例:
5 5
1 2 3 4 5
3 4 5 6 7
输出样例:
1 2
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define maxsize 10000
using namespace std;
typedef struct
{
int data[maxsize];
int length;
}sqlist;
int main()
{
sqlist *c=(sqlist*)malloc(sizeof(sqlist));
c->length=0;
int n,m;
cin>>n>>m;
sqlist *a=new sqlist;
sqlist *b=new sqlist;
for(int i=0;i<n;i++)
cin>>a->data[i];
a->length=n;
for(int j=0;j<m;j++)
cin>>b->data[j];
b->length=m;
int i=0,j=0,x=0;
while(i<a->length&&j<b->length)
{
if(a->data[i]<b->data[j])
{
c->data[x]=a->data[i];
x++;
i++;
}
else if(a->data[i]==b->data[j])
{
i++;
j++;
}
else
j++;
}
int y=i;
for(int k=y;k<a->length;k++)
{
c->data[x++]=a->data[k];
}
c->length=x;
if(c->length==0)
{
printf("0");
}
for(int i=0;i<c->length;i++)
{
printf("%d ",c->data[i]);
}
return 0;
}