离散题目6
Problem Description
bLue 最近忙于收集卡片,已知可收集的卡片一共有 n 种,每种卡片都有唯一的编号。 现在给出 bLue 已经收集到的 m 种卡片,你能告诉他剩下的没收集到的卡片都有什么吗?
Input
多组数据,到 EOF 结束(数据组数不超过 100)。
每组数据第一行输入 2 个整数 n (1 <= n <= 100), m (1 <= m <= n),分别表示卡片总的种类数和 bLue 已经收集到的种类数。
第二行输入 n 个从小到大给出的用空格分隔的整数,表示所有可收集卡片的编号。
第三行输入 m 个从小到大给出的用空格分隔的整数,表示 bLue 收集到的 m 种卡片的编号。
所有的编号都在 1~100 之间。
Output
对于每组数据,第一行输出一个整数,表示没有收集到的卡片有多少种,如果大于 0,则在下一行再按从小到大输出具体的编号。
Example Input
5 3 1 2 3 4 5 1 3 5 3 3 1 2 3 1 2 3
Example Output
2 2 4 0
Hint
第一组示例可以看作: U = {1, 2, 3, 4, 5},A = {1, 3, 5},答案即为 A 在 U 中的补集 C = {2, 4}。
代码
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int m, n;
int s[205], t[205], a[205];
while(cin>>m>>n)
{
for(int i = 0; i < m; i++)
{
cin>>s[i];
}
for(int i = 0; i < n; i++)
{
cin>>t[i];
}
sort(s, s+m);
sort(t, t+n);
int cnt = 0;
int q = 0;
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
if(t[j] == s[i])
break;
else
{
if(j == n-1)
{
cnt++;
a[q++] = s[i];
}
}
}
}
if(cnt == 0)
cout<<cnt<<endl;
else
{
cout<<cnt<<endl;
for(int p = 0; p < cnt; p++)
{
if(p == cnt-1)
cout<<a[p]<<endl;
else
cout<<a[p]<<" ";
}
}
}
}