Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int LeggiI()
- {
- int n;
- bool ok = false;
- do
- {
- ok = int.TryParse(Console.ReadLine(), out n);
- if (!ok)
- Console.WriteLine("Reinserisci il valore");
- } while (!ok);
- return n;
- }
- static void Main(string[] args)
- {
- int length1, length2, lengthMerge, minore = int.MaxValue, contatore1 = 0, contatore2 = 0, contatore3 = 0;
- bool ok;
- Console.WriteLine("Inserisci la lunghezza del primo vettore");
- do
- {
- ok = int.TryParse(Console.ReadLine(), out length1);
- if (!ok || length1 <= 0)
- Console.WriteLine("Errore, reinserisci il valore");
- } while (!ok || length1 <= 0);
- Console.WriteLine();
- Console.WriteLine();
- Console.WriteLine("Inserisci la lunghezza del secondo vettore");
- do
- {
- ok = int.TryParse(Console.ReadLine(), out length2);
- if (!ok || length2 <= 0)
- Console.WriteLine("Errore, reinserisci il valore");
- } while (!ok || length2 <= 0);
- Console.WriteLine();
- Console.WriteLine();
- lengthMerge = length1 + length2;
- int[] arr1 = new int[length1];
- int[] arr2 = new int[length2];
- int[] arrMerge = new int[lengthMerge];
- for (int i = 0; i < arr1.Length; i++)
- {
- Console.WriteLine("Inserisci il " + (i + 1) + " ^numero: ");
- arr1[i] = LeggiI();
- }
- Console.WriteLine();
- Console.WriteLine();
- for (int i = 0; i < arr2.Length; i++)
- {
- Console.WriteLine("Inserisci il " + (i + 1) + "^numero: ");
- arr2[i] = LeggiI();
- }
- bool flag;
- while (contatore1 < arr1.Length || contatore2 < arr2.Length)
- {
- flag = false;
- if (contatore1 < arr1.Length)
- {
- if (arr1[contatore1] < minore)
- {
- minore = arr1[contatore1++];
- flag = true;
- }
- }
- if (contatore2 < arr2.Length)
- {
- if (arr2[contatore2] < minore)
- {
- minore = arr2[contatore2++];
- if (flag)
- contatore1--;
- }
- }
- arrMerge[contatore3++] = minore;
- minore = int.MaxValue;
- }
- for (int i = 0; i < arrMerge.Length; i++)
- {
- Console.Write(arrMerge[i] + " ");
- }
- Console.ReadLine();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement