Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.82 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading;
  4.  
  5. public class Program
  6. {
  7.     public static void Main()
  8.     {
  9.         string[] inp = Console.ReadLine().Split();
  10.        
  11.         int n = int.Parse(inp[0]);
  12.         int m = int.Parse(inp[1]);
  13.         var a1 = ReadArray(n);
  14.         var a2 = ReadArray(m);
  15.  
  16.         var sw = new Stopwatch();
  17.  
  18.         sw.Start();
  19.         var res = CountSameEntries()
  20.         sw.Stop();
  21.  
  22.         Console.WriteLine(res);
  23.  
  24.  
  25.         // получение прошедшего времени.
  26.         TimeSpan ts = sw.Elapsed;
  27.                 // привидение времени в правильный формат и вывод его на экран.
  28.         string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
  29.             ts.Hours, ts.Minutes, ts.Seconds,
  30.             ts.Milliseconds / 10);
  31.         Console.WriteLine("Время выполнения: " + elapsedTime);
  32.         // получение занимаемой памяти и вывод её на экран.
  33.         var memory = (Process.GetCurrentProcess().WorkingSet64 / 1024 / 1024).ToString() + " MB";
  34.         Console.WriteLine("Объем занимаемой памяти (Мб): " + memory);
  35.  
  36.         Console.ReadKey();
  37.     }
  38.  
  39.     static int CountSameEntries(int[] a, int[] b)
  40.     {
  41.        
  42.         int counter = 0;
  43.         int i = 0; int k = 0;
  44.  
  45.         while (i < a.Length && k < b.Length)
  46.         {
  47.             if (a[i] == b[k])
  48.             {
  49.                 counter++; i++; k++;
  50.             }
  51.             else if (a[i] < b[k]) i++;
  52.             else k++;
  53.         }
  54.  
  55.         return counter;
  56.     }
  57.  
  58.     static int[] ReadArray(int size)
  59.     {
  60.         var array = new int[size];
  61.  
  62.         for (int i = 0; i < size; i++)
  63.             array[i] = int.Parse(Console.ReadLine());
  64.         return array;
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement