D. 交集
Time Limit: 4000msMemory Limit: 65535KB 64-bit integer IO format: %lld Java class name: Main
Submit Status PID: 1045
给出两个整数集合,求它们的交集,按照从小到大的顺序依次输出.
Input
输入数据有多组.
输入文件第一行给出一个T代表有T组数据.
第二行开始有T组数据,每组数据占两行,每行对应一个集合,每行首先是一个整数N(N<=10000)代表集合有N个整数,接着一个空格后给出了N个不同整数(每个数都在32位整数的范围内),为集合的元素,元素之间使用空格隔开.
Output
对于每一组数据,单独使用一行输出所给两个集合交集的元素,按从小到大的顺序依次输出,元素之间用1个空格隔开.如果交集为空,则输出一个字符串“empty”.每行末尾不要有空格.
Sample Input
2
3 1 2 3
3 3 2 1
2 1 5
5 5 6 3 4 7
Sample Output
1 2 3
5
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int main()
{
long long a[10000];
long long b[10000];
long long c[10000];
int p;
scanf("%d",&p);
while(p--)
{
int i,j,s=0;
int g,f;
scanf("%d",&g);
for(i=0; i<g; i++)
scanf("%lld",&a[i]);
scanf("%d",&f);
for(j=0; j<f; j++)
scanf("%lld",&b[j]);
for(j=0; j<f; j++)
for(i=0; i<g; i++)
{
if(a[i]==b[j])
{
c[s]=a[i];
s++;
}
}
if(s==0)
{
printf("empty\n");
continue;
}
sort(c,c+s);
for(i=0; i<s-1; i++)
printf("%lld ",c[i]);
printf("%lld\n",c[s-1]);
}
return 0;
}
我发现我特别傻。。。。。数组开小了一位,然后各种runtime error