Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. public static int LeggiI()
  2. {
  3. int n;
  4. bool ok = false;
  5.  
  6. do
  7. {
  8. ok = int.TryParse(Console.ReadLine(), out n);
  9. if (!ok)
  10. Console.WriteLine("Reinserisci il valore");
  11. } while (!ok);
  12. return n;
  13. }
  14. static void Main(string[] args)
  15. {
  16. int length1, length2, lengthMerge, minore = int.MaxValue, contatore1 = 0, contatore2 = 0, contatore3 = 0;
  17. bool ok;
  18.  
  19. Console.WriteLine("Inserisci la lunghezza del primo vettore");
  20. do
  21. {
  22. ok = int.TryParse(Console.ReadLine(), out length1);
  23. if (!ok || length1 <= 0)
  24. Console.WriteLine("Errore, reinserisci il valore");
  25. } while (!ok || length1 <= 0);
  26.  
  27. Console.WriteLine();
  28. Console.WriteLine();
  29.  
  30. Console.WriteLine("Inserisci la lunghezza del secondo vettore");
  31. do
  32. {
  33. ok = int.TryParse(Console.ReadLine(), out length2);
  34. if (!ok || length2 <= 0)
  35. Console.WriteLine("Errore, reinserisci il valore");
  36. } while (!ok || length2 <= 0);
  37.  
  38. Console.WriteLine();
  39. Console.WriteLine();
  40.  
  41. lengthMerge = length1 + length2;
  42.  
  43. int[] arr1 = new int[length1];
  44. int[] arr2 = new int[length2];
  45. int[] arrMerge = new int[lengthMerge];
  46.  
  47. for (int i = 0; i < arr1.Length; i++)
  48. {
  49. Console.WriteLine("Inserisci il " + (i + 1) + " ^numero: ");
  50. arr1[i] = LeggiI();
  51. }
  52.  
  53. Console.WriteLine();
  54. Console.WriteLine();
  55.  
  56. for (int i = 0; i < arr2.Length; i++)
  57. {
  58. Console.WriteLine("Inserisci il " + (i + 1) + "^numero: ");
  59. arr2[i] = LeggiI();
  60. }
  61.  
  62. bool flag;
  63.  
  64. while (contatore1 < arr1.Length || contatore2 < arr2.Length)
  65. {
  66. flag = false;
  67. if (contatore1 < arr1.Length)
  68. {
  69. if (arr1[contatore1] < minore)
  70. {
  71. minore = arr1[contatore1++];
  72. flag = true;
  73. }
  74. }
  75. if (contatore2 < arr2.Length)
  76. {
  77. if (arr2[contatore2] < minore)
  78. {
  79. minore = arr2[contatore2++];
  80. if (flag)
  81. contatore1--;
  82. }
  83. }
  84.  
  85. arrMerge[contatore3++] = minore;
  86. minore = int.MaxValue;
  87. }
  88.  
  89. for (int i = 0; i < arrMerge.Length; i++)
  90. {
  91. Console.Write(arrMerge[i] + " ");
  92. }
  93.  
  94. Console.ReadLine();
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement