离散题目4

本文介绍了一个通过C++实现的程序,该程序用于解决给定两个非空整数集时如何找出这两个集合的交集问题。程序可以处理多组输入,并能够将交集中的元素按递增顺序输出。

think:
1后台数据似乎每组输入数据之后都有空格

sdut题目链接

离散题目4
Time Limit: 1000MS Memory Limit: 65536KB

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
Author

以下为accepted代码

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>

using namespace std;

int cmp(const void *a, const void *b)
{
    return ((*(int *)a) - (*(int *)b));
}

char st1[800000], st2[800000];

int main()
{
    double sum, f;
    int i, j, flag;
    int a[80000], b[80000], c[80000], tp, op, top;
    while(gets(st1) != NULL)
    {
        tp = op = top = 0;
        gets(st2);
        int len1 = strlen(st1);
        int len2 = strlen(st2);

        sum = 0.0, f = 1.0;
        for(i = 0;i < len1; i++)
        {
            if(st1[i] != ' ')
            {
                if(st1[i] == '-')
                    f = -1.0;
                else
                    sum  = sum*10+(st1[i]-'0');
            }
            else if(st1[i] == ' '&& ((st1[i+1] >= '0' && st1[i+1] <= '9') || (st1[i+1] == '-')))
            {
                a[op++] = sum*f;
                f = 1.0;
                sum = 0.0;
            }
        }
        a[op++] = sum*f;
        sum = 0.0;
        f = 1.0;
        for(i = 0; i < len2; i++)
        {
            if(st2[i] != ' ')
            {
                if(st2[i] == '-')
                    f = -1.0;
                else
                    sum = sum*10+(st2[i]-'0');
            }
            else if(st2[i] == ' ' && ((st2[i+1] >= '0' && st2[i+1] <= '9') || (st2[i+1] == '-')))
            {
                b[tp++] = sum*f;
                f = 1.0;
                sum = 0.0;
            }
        }
        b[tp++] = sum*f;
        sum = 0.0;
        f = 1.0;

        flag = 0;
        for(i = 0; i < op; i++)
        {
            for(j = 0; j < tp; j++)
            {
                if(a[i] == b[j])
                {
                    flag = 1;
                    c[top++] = a[i];
                }
            }
        }
        if(flag == 0)
            printf("NULL\n");
        else
        {
            qsort(&c[0], top, sizeof(c[0]), cmp);
            for(i = 0; i < top; i++)
            {
                printf("%d%c", c[i], i == top-1? '\n': ' ');
            }
        }
    }
    return 0;
}



/***************************************************
User name: 
Result: Accepted
Take time: 124ms
Take Memory: 240KB
Submit time: 2017-03-29 19:50:24
****************************************************/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值