UVA496 Simply Subsets【STL+集合运算】

After graduating from the University of Notre Dame, you obtained a job at Top Shelf Software, Inc., as an entry-level computer engineer. On the first day, your manager sits down with you and tasks you with the following job: “We want to see how well you understand computer programming and the abstract science behind it. As an evaluation for all of our new hires, we require them to write a program to determine the relationship between pairs of sets. I’m quite sure that you’ll do well; my confidence is high. Here’s a list of requirements for what the program should do. Good luck.”
Input
Your program should accept an even number of lines of text. Each pair of lines will represent two sets; the first line represents set A, the second line represents set B. Each line of text (set) will be a list of distinct integers.
Output
After each pair of lines has been read in, the sets should be compared and one of the following responses should be output:
• A is a proper subset of B
• B is a proper subset of A
• A equals B
• A and B are disjoint
• I’m confused!
Sample Input
55 27
55 27
9 24 1995
9 24
1 2 3
1 2 3 4
1 2 3
4 5 6
1 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!

问题链接UVA496 Simply Subsets
问题简述:给定两个整数集合(每个集合的输入1行,共2行),判定集合关系(相等,子集,不相交,其他)。
问题分析
    输入处理比较麻烦一些,编写一个函数来实现。使用计算集合交集(A∩B)的函数set_intersection()来实现,先算出2个集合的交,再进行判定。
    头文件algorithm.h中有若干集合运算的函数,例如函数set_union用于计算集合的并集(A∪B,Union of two sorted ranges)、函数set_intersection()用于计算集合的交集(A∩B,Intersection of two sorted ranges)、函数set_difference()用于计算集合的差集(A-B,Difference of two sorted ranges)和函数set_symmetric_difference()用于计算集合的对称差集(A△B=(A∪B)-(A∩B),不属于A∩B的元素的集合,Symmetric difference of two sorted ranges)。

函数 运算 含义 说明
set_union() A∪B Union of two sorted ranges
set_intersection() A∩B Intersection of two sorted ranges
set_difference() A-B Difference of two sorted ranges
set_symmetric_difference() A△B=(A∪B)-(A∩B) Symmetric difference of two sorted ranges 不属于A∩B的元素的集合

程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA496 Simply Subsets */

#include <bits/stdc++.h>

using namespace std;

const int N = 100;
int a[N], b[N];
vector<int> v(N);

int read( int d[], int *p )
{
    *p = 0;
    char ch;
    if(scanf("%d%c", &d[(*p)++], &ch) == EOF)
        return 0;
    while(ch != '\n')
        scanf("%d%c", &d[(*p)++], &ch);
    return 1;
}

int main()
{
    int n, m;
    while(read(a, &n)) {
        read(b, &m);

        sort(a, a + n);
        sort(b, b + m);

        int diff = set_intersection(a, a + n, b, b + m, v.begin()) - v.begin();
        if(diff == min(n, m)) {
            if (n < m) puts("A is a proper subset of B");
            else if (n > m) puts("B is a proper subset of A");
            else puts("A equals B");
        } else if (diff) puts("I'm confused!");
        else puts("A and B are disjoint");
    }

    return 0;
}
原创文章 2323 获赞 2382 访问量 269万+

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/105781550
今日推荐