当前位置: 代码迷 >> C语言 >> [求助]ACM题,合并两个集合的元素的代码
  详细解决方案

[求助]ACM题,合并两个集合的元素的代码

热度:205   发布时间:2007-08-07 14:02:31.0
[求助]ACM题,合并两个集合的元素的代码

{A} + {B}

Time Limit:5000MS Memory Limit:65536K
Total Submit:15 Accepted:1

Description

Given Two collections {A} and {B}.
Note: there are no two same elements in each collections.

Input

The problem has many test cases.
For each case,there are three lines.
The first line contains two integer n and m (0 < n,m <= 10000). n is the number of {A}'s elements, and m is the number of {B}'s elements.
The second line is the elements of {A}.
The third line is the elements of {B}.

Output

For each case, output one line containing all the elements in the combination of collections {A} and {B}. You should order the elements by ascend(from small one to big one).

Sample Input


1 2
1
2 3
1 2
1
1 2


Sample Output


1 2 3
1 2

我的代码
//07-8-7 zist 2507 {A} + {B}
#include <stdio.h>
void SortBullbe(int *a ,int n)
{
int i ,j ,k ,temp;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
{
if(a[k]>a[j])
{
k=j;
}
}
if(k!=i)
{
temp=a[i];
a[i]=a[k];
a[k]=temp;
}
}
}

main()
{
int i ,j ,k ,n ,m ,l ,count=0 ,flag=0;
int a[10005] ,b[10005] ,c[20005];
while(scanf("%d%d",&n,&m)!=EOF)
{
count=0 ;
flag=0;
for(i=0;i < n;i++)
{
scanf("%d",&a[i]);
}
SortBullbe(a ,n) ;
for(i=0;i < m;i++)
{
scanf("%d",&b[i]);
}
SortBullbe(b ,m) ;
if(a[n-1]<b[0])
{
for(i=0 ;i <n;i++)
{
printf("%d ",a[i]);
}
for(i=0 ;i <m;i++)
{
printf("%d ",b[i]);
}
}
else
{
k=0; l=0 ;
for(i=0 ;i < n;i++)
{
for(j=i+k;j < m;j++)
{
if(a[i] < b[j])
{
c[count++]=a[i];
break;
}
if(a[i]>b[j])
{
c[count++]=b[j];
l++ ;
continue;
}
if(a[i]==b[j])
{
c[count++]=a[i];
break;
}
}
if(l>0)
k=l;
if(j==m)
{
flag=1;
break;
}
}
if(flag==0)
{
for( ;j < m-1 ;j++)
{
c[count++]=b[j+1];
}
}
if(flag==1)
{
for( ;i < n;i++)
{
c[count++]=a[i];
}
}
for(i=0 ;i <count;i++)
{
printf("%d ",c[i]);
}
}
printf("\n");
}
}
大家帮忙看下错误,我在 本地调试的时候通过了的,可提交上去就是说我的answer error,我实在看不出错误了,大家帮忙看下,

[此贴子已经被作者于2007-8-7 14:05:32编辑过]

搜索更多相关的解决方案: ACM  元素  代码  

----------------解决方案--------------------------------------------------------
貌似取个标题还那么难的,一直发不上来 。大家帮忙看下我的错误
----------------解决方案--------------------------------------------------------
说思路吧,看代码很累的...................
最简单的想法就是把a,b存在一个数组,排序,然后扫描去重..
如果会stl的话,就直接用set做了
----------------解决方案--------------------------------------------------------
我的是这个思路啊,调试的时候没什么问题的,但提交的时候就是说我answer error,很是郁闷啊
----------------解决方案--------------------------------------------------------
就没哪个帮我看下代码啊!~
虽然我没写注释,也不要这样对我啊

----------------解决方案--------------------------------------------------------

建立判定数组
bool s[MAXNUM]={0};

顺序一边读入一边处理,读入数据Mn后s[Mn]=1;
然后循环判断输出 if(s[Mn]) printf("%d",Mn);


----------------解决方案--------------------------------------------------------
以下是引用卧龙孔明在2007-8-7 15:20:38的发言:

建立判定数组
bool s[MAXNUM]={0};

顺序一边读入一边处理,读入数据Mn后s[Mn]=1;
然后循环判断输出 if(s[Mn]) printf("%d",Mn);

这要建立在数据小的情况下才能用吧,
不然会超空间的


----------------解决方案--------------------------------------------------------
以下是引用liulanghan在2007-8-7 14:30:06的发言:
我的是这个思路啊,调试的时候没什么问题的,但提交的时候就是说我answer error,很是郁闷啊

你的代码就算不wa,也会tle的,你的排序是O(n^2)的 -_-


----------------解决方案--------------------------------------------------------
以下是引用cwande在2007-8-7 15:35:52的发言:

这要建立在数据小的情况下才能用吧,
不然会超空间的

bool占空间很小,Memory Limit:65536K 所以不会超空间的

[此贴子已经被作者于2007-8-7 15:59:40编辑过]


----------------解决方案--------------------------------------------------------
偶是说比如maxnum=2^31-1................
----------------解决方案--------------------------------------------------------
  相关解决方案