fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int compare(const void* a, const void* b){
  5. return *(int*)a - *(int*)b;
  6. }
  7.  
  8. int main(){
  9. int n,m; scanf("%d %d",&n,&m);
  10. int a[n],b[m];
  11. int a1[1005] = {0};
  12. int b1[1005] = {0};
  13. for(int i = 0; i < n; i++){
  14. scanf("%d",&a[i]);
  15. a1[a[i]]++;
  16. }
  17. for(int i = 0; i < m; i++){
  18. scanf("%d",&b[i]);
  19. b1[b[i]]++;
  20. }
  21. qsort(a,n,sizeof(int),compare);
  22. qsort(b,m,sizeof(int),compare);
  23. int giao[1005] = {0}, cntgiao = 0;
  24. int a_b[1005] = {0}, cnta_b = 0;
  25. int b_a[1005] = {0}, cntb_a = 0;
  26. for(int i = 0; i < n; i++){
  27. if(a1[a[i]] == 1 && b1[a[i]] == 1){
  28. giao[cntgiao] = a[i];
  29. cntgiao++;
  30. a1[a[i]] = 0;
  31. b1[a[i]] = 0;
  32. }
  33. }
  34. for(int i = 0; i < m; i++){
  35. if(a1[b[i]] == 1 && b1[b[i]] == 1){
  36. giao[cntgiao] = b[i];
  37. cntgiao++;
  38. a1[b[i]] = 0;
  39. b1[b[i]] = 0;
  40. }
  41. }
  42. for(int i = 0; i < n; i++){
  43. if(a1[a[i]] == 1){
  44. a_b[cnta_b] = a[i];
  45. cnta_b++;
  46. }
  47. }
  48. for(int i = 0; i < m; i++){
  49. if(b1[b[i]] == 1){
  50. b_a[cntb_a] = b[i];
  51. cntb_a++;
  52. }
  53. }
  54. for(int i = 0; i < cntgiao; i++)
  55. printf("%d ",giao[i]);
  56. printf("\n");
  57.  
  58. for(int i = 0; i < cnta_b; i++)
  59. printf("%d ",a_b[i]);
  60. printf("\n");
  61.  
  62. for(int i = 0; i < cntb_a; i++)
  63. printf("%d ",b_a[i]);
  64. printf("\n");
  65.  
  66. return 0;
  67. }
  68.  
Success #stdin #stdout 0.01s 5304KB
stdin
5 6
2 6 5 4 7
3 6 5 8 9 7
stdout
5 6 7 
2 4 
3 8 9