每组测试数据占5行,第1行是和s (1 <= s <= 10000),第2行是一个正整数a (1 <= a <= 10000),表示A中元素的数目。第3行是a个正整数,每个正整数不超过10000,表示A中的元素。第4行是一个正整数b (1 <= b <= 10000),表示B中元素的数目。第5行是b个正整数,每个正整数不超过10000,表示B中的元素。
注意:这里的集合和数学书上定义的集合有一点点区别——集合内可能包含相等的正整数。
2 99 2 49 49 2 50 50 11 9 1 2 3 4 5 6 7 8 9 10 10 9 8 7 6 5 4 3 2 1
4 9
sort函数用法:http://blog.csdn.net/fly_yr/article/details/18894549
#include<stdio.h>
#include<algorithm>
using namespace std;
int len_a,len_b;
int a[10000],b[10000];
int s;
int calculate()
{
int i,j;
int num = 0;
for(i = 0;i < len_a;i ++)
{
for(j = 0;j < len_b;j ++)
{
if(a[i] + b[j] == s)
{
num ++;
}
if(a[i] + b[j] > s)
break;
}
}
return num;
}
int main()
{
int times;//测试次数
int i,j;
scanf("%d",×);
while(times--)
{
scanf("%d",&s);
scanf("%d",&len_a);
for(i = 0;i < len_a;i ++)
scanf("%d",&a[i]);
scanf("%d",&len_b);
for(j = 0;j < len_b;j ++)
scanf("%d",&b[j]);
sort(a,a+len_a);
sort(b,b+len_b);
printf("%d\n",calculate());
}
}