(HDOJ 1412){A} + {B}

 

{A} + {B}

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)


Problem Description
给你两个集合,要求{A} + {B}.
注:同一个集合中不会有两个相同的元素.
 

Input
每组输入数据分为三行,第一行有两个数字n,m(0<n,m<=10000),分别表示集合A和集合B的元素个数.后两行分别表示集合A和集合B.每个元素为不超出int范围的整数,每个元素之间有一个空格隔开.
 

Output
针对每组数据输出一行数据,表示合并后的集合,要求从小到大输出,每个元素之间有一个空格隔开.
 

Sample Input
1 2

2 3 

1 2 

1 2

 

Sample Output
1 2 3

1 2

 

  Sample code:

  #include<stdio.h>

#include<math.h>
#include
<string.h>
#include
<stdlib.h>

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

int main()
{
     
int m,n;
     
while(scanf("%d %d",&m,&n)!=EOF)
     {
          
int i,j,flag=0;
          
int a[10001],b[10001];
          
for(i=0; i<m; i++)
          {
               scanf(
"%d",&a[i]);
             }
             qsort(a,m,
sizeof(a[0]),cmp);
             
for(i=0; i<n; i++)
          {
               scanf(
"%d",&b[i]);
             }
             qsort(b,n,
sizeof(b[0]),cmp);
             i
=j=0;
             
while(i<&& j<n)
             {
                  
if(a[i]==b[j])
                  { 
                    
if(!flag)
                    {
                         printf(
"%d",a[i]);
                         i
++; j++;
                         flag
=1;
                       }
                       
else
                       {
                        printf(
" %d",a[i]);
                        i
++; j++;
                       }
                     }
                     
else if(a[i]<b[j])
                     {
                          
if(!flag)
                    {
                         printf(
"%d",a[i]);
                         i
++;
                         flag
=1;
                       }
                       
else
                       {
                           printf(
" %d",a[i]);
                           i
++;
                          }
                        }
                        
else
                        {
                             
if(!flag)
                    {
                         printf(
"%d",b[j]);
                         j
++;
                         flag
=1;
                       }
                       
else
                       {
                              printf(
" %d",b[j]);
                              j
++;
                             }
                        }
                }
                
if(i==m)
                {
                     
for(; j<n; j++)
                     {
                          printf(
" %d",b[j]);
                        }
                }
                
else
                {
                     
for(; i<m; i++)
                     {
                          printf(
" %d",a[i]);
                        }
                }
            printf(
"\n");
        }
}

 

 

posted @ 2011-05-20 00:50  cpoint  阅读(651)  评论(0编辑  收藏  举报
浏览次数:travelocity promotion codes