当前位置: 代码迷 >> C语言 >> 征求最简单的解决方案?
  详细解决方案

征求最简单的解决方案?

热度:222   发布时间:2007-08-10 17:57:31.0
征求最简单的解决方案?

如下的题:
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) 也就是两个的差集的并集
可是这两个方法的实现都很麻烦,有没有更简单的实现呢?

搜索更多相关的解决方案: 方案  征求  

----------------解决方案--------------------------------------------------------
我们以前讨论过类似题的,楼主看这个帖子
http://bbs.bc-cn.net/viewthread.php?tid=160933&star=at#
----------------解决方案--------------------------------------------------------
谢谢,我看看
----------------解决方案--------------------------------------------------------
好像没有什么最终的解决方案?
而且和我的问题有差距啊,我是问:除了我说的两种方法,还有没有更简单的方法?
----------------解决方案--------------------------------------------------------
归并会不会
----------------解决方案--------------------------------------------------------
会啊。
----------------解决方案--------------------------------------------------------

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


----------------解决方案--------------------------------------------------------
哪里啊?呵呵,我不太明白,你能不能说得明白些啊?谢谢
----------------解决方案--------------------------------------------------------
不能啊,怎么解决,请教高手,大家都来讨论讨论啊,共同进步啊!
----------------解决方案--------------------------------------------------------
回复:(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;
}


----------------解决方案--------------------------------------------------------
  相关解决方案