Advertisement
STANAANDREY

phones2020

Oct 21st, 2023
807
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Scanner;
  5. import java.util.Random;
  6.  
  7. class Phone {
  8.     private String name;
  9.     private String[] callers;
  10.     private int len;
  11.  
  12.     public Phone(final String name) {
  13.         this.name = name;
  14.         callers = new String[100];
  15.     }
  16.  
  17.     private boolean add(final String name) {
  18.         if (callers.length == len) {
  19.             return false;
  20.         }
  21.         callers[len++] = name;
  22.         return true;
  23.     }
  24.  
  25.     public boolean call(final Phone other) {
  26.         return other.add(this.name);
  27.     }
  28.  
  29.     public Integer nrOfCalls(final String name) {
  30.         int cnt = 0;
  31.         for (int i = 0; i < len; i++) {
  32.             if (callers[i].equals(name)) {
  33.                 cnt++;
  34.             }
  35.         }
  36.         return cnt;
  37.     }
  38.  
  39.     @Override
  40.     public String toString() {
  41.         String string = "name: " + name + "[ ";
  42.         for (int i = 0; i < len; i++) {
  43.             string += callers[i] + " ";
  44.         }
  45.         string += " ]";
  46.         return string;
  47.     }
  48. }
  49.  
  50. public class Main {
  51.     public static void main(String[] args) throws IOException {
  52.         var scanner = new Scanner(System.in);
  53.         var br = new BufferedReader(new InputStreamReader(System.in));
  54.         int n = scanner.nextInt();
  55.         var phones = new Phone[n];
  56.         for (int i = 0; i < n; i++) {
  57.             String name = br.readLine();
  58.             phones[i] = new Phone(name);
  59.         }
  60.         int a = scanner.nextInt();
  61.         var random = new Random();
  62.         for (int i = 0; i < a; i++) {
  63.             int x, y;
  64.             do {
  65.                 x = Math.abs(random.nextInt() % n);
  66.                 y = Math.abs(random.nextInt() % n);
  67.             } while (x == y);
  68.             phones[x].call(phones[y]);
  69.         }
  70.         String name = br.readLine();
  71.         for (int i = 0; i < n; i++) {
  72.             System.out.println(phones[i] + "\n" + phones[i].nrOfCalls(name) + "\n");
  73.         }
  74.     }
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement