fork download
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int main() {
  6. int *A, *B;
  7. int i = 0, j = 0;
  8. int x, y;
  9.  
  10. // Alokasi memori
  11. A = (int *)malloc(2000 * sizeof(int));
  12. B = (int *)malloc(2000 * sizeof(int));
  13.  
  14. // Input array A sampai -9
  15. while (1) {
  16. scanf("%d", &x);
  17.  
  18. if (x == -9)
  19. break;
  20.  
  21. A[i++] = x;
  22. }
  23.  
  24. // Input array B sampai -9
  25. while (1) {
  26. scanf("%d", &y);
  27.  
  28. if (y == -9)
  29. break;
  30.  
  31. B[j++] = y;
  32. }
  33.  
  34. // Merge dua array terurut
  35. int p = 0, q = 0;
  36.  
  37. while (p < i && q < j) {
  38. if (A[p] < B[q]) {
  39. printf("%d ", A[p]);
  40. p++;
  41. } else {
  42. printf("%d ", B[q]);
  43. q++;
  44. }
  45. }
  46.  
  47. // Sisa elemen A
  48. while (p < i) {
  49. printf("%d ", A[p]);
  50. p++;
  51. }
  52.  
  53. // Sisa elemen B
  54. while (q < j) {
  55. printf("%d ", B[q]);
  56. q++;
  57. }
  58.  
  59. // Free memory
  60. free(A);
  61. free(B);
  62.  
  63. return 0;
  64. }
  65.  
  66.  
Success #stdin #stdout 0s 5312KB
stdin
2 5 9 -9
4 8 10 15 20 -9
stdout
2 4 5 8 9 10 15 20