1.链接地址:
http://bailian.openjudge.cn/practice/2792
2.题目:
- 总Time Limit:
- 3000ms
- Memory Limit:
- 65536kB
- Description
- 给出2个正整数集合A = {pi | 1 <= i <= a},B = {qj | 1 <= j <= b}和一个正整数s。问题是:使得pi + qj = s的不同的(i, j)对有多少个。
- Input
- 第1行是测试数据的组数n,后面跟着n组测试数据。
每组测试数据占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中的元素。
注意:这里的集合和数学书上定义的集合有一点点区别——集合内可能包含相等的正整数。- Output
- n行,每行输出对应一个输入。输出应是一个非负整数。
- Sample Input
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- Sample Output
4 9
3.思路:
标记法,记录每个数字出现的次数,再计算
4.代码:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 5 using namespace std; 6 7 int main() 8 { 9 //freopen("C://input.txt","r",stdin); 10 11 int temp,i; 12 13 int n; 14 cin >> n; 15 16 while(n--) 17 { 18 int s,a,b; 19 20 cin >> s; 21 22 int *arr_a = new int[s]; 23 int *arr_b = new int[s]; 24 25 memset(arr_a,0,sizeof(int) * s); 26 memset(arr_b,0,sizeof(int) * s); 27 28 cin >> a; 29 while(a--) 30 { 31 cin >> temp; 32 if(temp <= s) ++(arr_a[temp - 1]); 33 } 34 35 cin >> b; 36 while(b--) 37 { 38 cin >> temp; 39 if(temp <= s) ++(arr_b[temp - 1]); 40 } 41 42 int count = 0; 43 for(i = 0; i < s; ++i) 44 { 45 count += arr_a[i] * arr_b[s - i - 2]; 46 } 47 48 cout << count << endl; 49 50 delete [] arr_a; 51 delete [] arr_b; 52 } 53 54 return 0; 55 }
OpenJudge 2792 集合加法,布布扣,bubuko.com
原文:http://www.cnblogs.com/mobileliker/p/3584664.html