Advertisement
Ansaid

Yandex2

Feb 7th, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         Scanner in = new Scanner(System.in);
  8.  
  9.         int countN = in.nextInt();
  10.         int[] arrA = new int[countN];
  11.         for(int i = 0; i < countN; i++) {
  12.             arrA[i] = in.nextInt();
  13.         }
  14.  
  15.         int countM = in.nextInt();
  16.         int[] arrB= new int[countM];
  17.         for(int i = 0; i < countM; i++) {
  18.             arrB[i] = in.nextInt();
  19.         }
  20.  
  21.         int[] arrItog = new int[countM];
  22.  
  23.         int min;
  24.         int minIndex = 0;
  25.         for(int i = 0; i < countM; i++) {
  26.             min = 50001;
  27.             for(int j = 0; j < countN; j++) {
  28.                 if (min >= Math.abs(arrA[j] - arrB[i])) {
  29.                     min = Math.abs(arrA[j] - arrB[i]);
  30.                     minIndex = j;
  31.                 }
  32.             }
  33.             arrItog[i] = arrA[minIndex];
  34.         }
  35.  
  36.         for(int i = 0; i < countM; i++) {
  37.             if(i == countM - 1) {
  38.                 System.out.print(arrItog[i]);
  39.             } else {
  40.                 System.out.println(arrItog[i]);
  41.             }
  42.         }
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement