FJNU.1980
Description 给定两个集合A、B,集合内的任一元素x满足1 ≤ x ≤ 109,并且每个集合的元素个数不大于105。我们希望求出A、B之间的关系。 任 务 :给定两个集合的描述,判断它们满足下列关系的哪一种: A是B的一个真子集,输出“A is a proper subset of B” B是A的一个真子集,输出“B is a proper subset of A” A和B是同一个集合,输出“A equals B” A和B的交集为空,输出“A and B are disjoint” 上述情况都不是,输出“I'm confused!”
Input 输入有两行,分别表示两个集合,每行的第一个整数为这个集合的元素个数(至少一个),然后紧跟着这个集合的元素(均为不同的正整数)
Output 输出只有一行,就是A、B的关系。
Sample Input 2 55 27 2 55 27
3 9 24 19 2 9 24
3 1 2 3 4 1 2 3 4
3 1 2 3 3 4 5 6
2 1 2 2 2 3
Sample Output A equals B
B is a proper subset of A
A is a proper subset of B
A and B are disjoint
I'm confused!
Source 2007'福建师范大学低年级学生程序设计大赛
My Program
#include < iostream > using namespace std; void sort( int a[], int n) ... { int temp; int i,j; for (i = 0 ;i < n;i ++ ) for (j = i + 1 ;j < n;j ++ ) if (a[i] > a[j]) ... { temp = a[i]; a[i] = a[j]; a[j] = temp; } }void del( int a[], int & n) ... { int i,j,k; for (i = 0 ;i < n;i ++ ) ... { k = 0 ; while (a[i] == a[i + k + 1 ]) k ++ ; for (j = 0 ;j < k;j ++ ) a[i + j + 1 ] = a[i + j + k + 1 ]; n -= k; } }void count( int a[], int b[], int n, int m) ... { sort(a,n);del(a,n); sort(b,m);del(b,m); int i = 0 ,j = 0 ,k = 0 ; while (i < n && j < m) ... { if (a[i] != b[j]) ... { if ((a[i] < b[i]) && i < n && k !=- 1 && (a[i + 1 ] <= b[i])) ... { i ++ ; k = 1 ; } else if ((a[i] < b[i]) && i < n && k !=- 1 && (a[i + 1 ] <= b[i])) ... { k =- 1 ; j ++ ; } else break ; } i++ ; j ++ ; } if ((i == n) && (j == m) && (i == j)) ... { if (k == 0 ) cout << " A equals B " << endl; else if (k == 1 ) cout << " A is a proper subset of B " << endl; else if (k ==- 1 ) cout << " B is a proper subset of A " << endl; return ; } if ((i < n) && (j == m) && (n > m)) ... { cout << " B is a proper subset of A " << endl; return ; } if ((i == n) && (j < m) && (n < m)) ... { cout << " A is a proper subset of B " << endl; return ; } if ((i == n) && (j == m)) ... { cout << " B is a proper subset of A " << endl; return ; } if ((i == n) && (j < m) && (k != 1 )) ... { cout << " B is a proper subset of A " << endl; return ; } if ((i == n) && (j == m)) ... { cout << " A is a proper subset of B " << endl; return ; } if ((i < n) && (j == m) && (k !=- 1 )) ... { cout << " A is a proper subset of B " << endl; return ; } for (i = 0 ;i < n;i ++ ) for (j = 0 ;j < m;j ++ ) if (a[i] == b[j]) ... { cout << " I'm confused! " << endl; return ; } cout<< " A and B are disjoint " << endl; } int main() ... { int a[ 110 ],b[ 110 ]; int n,m,i; cin >> n; for (i = 0 ;i < n;i ++ ) cin >> a[i]; cin >> m; for (i = 0 ;i < m;i ++ ) cin >> b[i]; count(a,b,n,m); return 0 ; }
YOYO's Note: 在第一个小时AC了两题之后接下来三个小时都无所收获, 本打算放弃这题因为看不懂它的输入输出,这个时候老师说把它REJUDGE了,很多人AC了…… 于是就开始猛看,终于看懂了,在非常紧张的状态下花了半个多钟头终于把那9个IF写完了 Orz.. 代码很乱。。不过幸运的是一次AC,如果WA一定会心烦意乱,不知道最后结果会怎样……