离散题目4
2017-06-19 22:50
176 查看
离散题目4
Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic
Problem Description
题目给出两个非空整数集,请写出程序求两个集合的交集。
Input
多组输入,每组输入包括两行,第一行为集合A的元素,第二行为集合B的元素。具体参考示例输入。 每个集合元素个数不大于3000,每个元素的绝对值不大于2^32 - 1。
Output
每组输入对应一行输出,为A、B的交集,如果交集为空输出”NULL”,否则交集的元素按递增顺序输出,每个元素之间用空格分割。
Example Input
1 2 3 4 5
1 5 3 6 7
1 2 4 5 3
6 7 8 9 10
Example Output
1 3 5
NULL
Hint
Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic
Problem Description
题目给出两个非空整数集,请写出程序求两个集合的交集。
Input
多组输入,每组输入包括两行,第一行为集合A的元素,第二行为集合B的元素。具体参考示例输入。 每个集合元素个数不大于3000,每个元素的绝对值不大于2^32 - 1。
Output
每组输入对应一行输出,为A、B的交集,如果交集为空输出”NULL”,否则交集的元素按递增顺序输出,每个元素之间用空格分割。
Example Input
1 2 3 4 5
1 5 3 6 7
1 2 4 5 3
6 7 8 9 10
Example Output
1 3 5
NULL
Hint
#include<bits/stdc++.h> using namespace std; int main() { string s1,s2; stringstream ss; int a[3500],b[3500]; int key[3500]; memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); while(getline(cin,s1)) { getline(cin,s2); ss.clear(); ss.str(s1); int l1 = 0,l2 = 0,l3= 0; while(true) { if(ss.fail()) break; ss >> a[l1++]; } ss.clear(); ss.str(s2); while(true) { if(ss.fail()) break; ss>>b[l2++]; } for(int i = 0;i<l1-1;i++) { for(int j = 0;j<l2-1;j++) { if(a[i]==b[j]) { key[l3++] = a[i]; break; } } } sort(key,key+l3); if(l3==0) { printf("NULL"); } else { for(int i = 0;i<l3;i++) { if(i==0) { printf("%d",key[i]); } else { printf(" %d",key[i]); } } } cout<<endl; } }