Advertisement
daniel_079

AIgorithms1.4

Sep 12th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.93 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Algorithms3 {
  4.     public static void main (String[] args) {
  5.         Scanner in = new Scanner (System.in);
  6.         System.out.println("Enter lenght of the first array");
  7.         int n = in.nextInt();
  8.         System.out.println("Enter elements of the first array");
  9.         int []a = new int [n];
  10.         for (int i = 0; i < n; i++) {
  11.             a[i] = in.nextInt(); // заполняем первый массив
  12.         }
  13.         System.out.println("Enter lenght of the second array");
  14.         int k = in.nextInt();
  15.         System.out.println("Enter elements of the second array");
  16.         int []b = new int [k];
  17.         for (int j = 0; j < k; j++) {
  18.             b[j] = in.nextInt(); // заполняем второй массив
  19.         }
  20.         int i = 0;
  21.         int j = 0;
  22.         int counter = 0;
  23.         while ((i < n) && (j < k)) {
  24.             if (a[i] == b[j]) {
  25.                 counter++;
  26.                 i++;
  27.                 j++;
  28.             } else if (a[i] > b[j]) {
  29.                 j++;
  30.             } else {
  31.                 i++;
  32.             }
  33.         }
  34.         System.out.println(counter);
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement