| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 848 人关注过本帖
标题:征求最简单的解决方案?
只看楼主 加入收藏
ConZhang
Rank: 1
来 自:北京
等 级:新手上路
帖 子:282
专家分:0
注 册:2007-8-7
收藏
 问题点数:0 回复次数:11 
征求最简单的解决方案?

如下的题:
The symmetric difference of two sets is the set of elements belonging to one but not both of the two sets. For example, if we have two sets A = {1,2,3,4,5} and B = {3,4,5,6,7,8}, then the symmetric difference of A and B is the set {1,2,6,7,8}.

Given two sets of positive integers, display their symmetric difference.

Input

A positive integer will denote the number of cases. Both sets will be input from a single line. A zero (0) marks the end of each set. There will be no more than 20 numbers in each set, and no number within a set will be repeated. Each number in a set is a positive integer less than 65535.

Output

The set of integers making up the symmetric difference of the two sets. The numbers within a set may be sorted from smaller to bigger.

Sample Input
3
1 2 3 4 5 0 3 4 5 6 7 8 0
1 2 3 0 1 2 3 0
129 34 5 6 7 0 129 0
Sample Output
{1,2,6,7,8}
{}
{5,6,7,34}

其实求symmetric集我自己有几个办法:
1.并集-交集
2.(p-q)V(q-p) 也就是两个的差集的并集
可是这两个方法的实现都很麻烦,有没有更简单的实现呢?

搜索更多相关主题的帖子: 方案 征求 
2007-08-10 17:57
liulanghan
Rank: 1
等 级:禁止访问
帖 子:104
专家分:0
注 册:2007-5-5
收藏
得分:0 
我们以前讨论过类似题的,楼主看这个帖子
http://bbs.bc-cn.net/viewthread.php?tid=160933&star=at#
2007-08-10 18:30
ConZhang
Rank: 1
来 自:北京
等 级:新手上路
帖 子:282
专家分:0
注 册:2007-8-7
收藏
得分:0 
谢谢,我看看
2007-08-10 18:35
ConZhang
Rank: 1
来 自:北京
等 级:新手上路
帖 子:282
专家分:0
注 册:2007-8-7
收藏
得分:0 
好像没有什么最终的解决方案?
而且和我的问题有差距啊,我是问:除了我说的两种方法,还有没有更简单的方法?
2007-08-10 18:37
leeco
Rank: 4
等 级:贵宾
威 望:10
帖 子:1029
专家分:177
注 册:2007-5-10
收藏
得分:0 
归并会不会
2007-08-10 18:40
ConZhang
Rank: 1
来 自:北京
等 级:新手上路
帖 子:282
专家分:0
注 册:2007-8-7
收藏
得分:0 
会啊。
2007-08-10 18:42
liulanghan
Rank: 1
等 级:禁止访问
帖 子:104
专家分:0
注 册:2007-5-5
收藏
得分:0 

最后一个人的代码你看了吗???
不能解决你的问题么??

2007-08-10 18:44
ConZhang
Rank: 1
来 自:北京
等 级:新手上路
帖 子:282
专家分:0
注 册:2007-8-7
收藏
得分:0 
哪里啊?呵呵,我不太明白,你能不能说得明白些啊?谢谢
2007-08-10 18:46
ConZhang
Rank: 1
来 自:北京
等 级:新手上路
帖 子:282
专家分:0
注 册:2007-8-7
收藏
得分:0 
不能啊,怎么解决,请教高手,大家都来讨论讨论啊,共同进步啊!
2007-08-10 19:01
飞天佳星
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2007-8-9
收藏
得分:0 
回复:(ConZhang)征求最简单的解决方案?

没试了一下,好像是对的吧。
#include<iostream>
using namespace std;
#include<algorithm>
int k,a[20],b[20],c[20],d[20];
int BinSearch(int R[],int K)
{
int low=0,high=k-1,mid;
while(low<=high){
mid=(low+high)/2;
if(R[mid]==K) return mid;
if(R[mid]>K)
high=mid-1;
else
low=mid+1;
}
return -1;
}
int main()
{
int n,m,cas,i,p,l;
cin>>cas;
while(cas--)
{ k=l=p=0;
memset(d,0,sizeof(d));
while(cin>>a[l]&&a[l])l++;
while(cin>>b[k]&&b[k])k++;
sort(&b[0],&b[k]);
for(i=0;i<l;i++)
if(BinSearch(b,a[i])!=-1)d[BinSearch(b,a[i])]=1;
else c[p++]=a[i];
for(i=0;i<k;i++)
if(d[i]==0) c[p++]=b[i];
if(p==0)cout<<"{}\n";
else {
sort(&c[0],&c[p]);
cout<<"{"<<c[0];
for(i=1;i<p;i++)
cout<<","<<c[i];
cout<<"}\n";
}
}
return 0;
}


2007-08-11 11:46
快速回复:征求最简单的解决方案?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012489 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved