Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Scanner;
- import java.util.Random;
- class Phone {
- private String name;
- private String[] callers;
- private int len;
- public Phone(final String name) {
- this.name = name;
- callers = new String[100];
- }
- private boolean add(final String name) {
- if (callers.length == len) {
- return false;
- }
- callers[len++] = name;
- return true;
- }
- public boolean call(final Phone other) {
- return other.add(this.name);
- }
- public Integer nrOfCalls(final String name) {
- int cnt = 0;
- for (int i = 0; i < len; i++) {
- if (callers[i].equals(name)) {
- cnt++;
- }
- }
- return cnt;
- }
- @Override
- public String toString() {
- String string = "name: " + name + "[ ";
- for (int i = 0; i < len; i++) {
- string += callers[i] + " ";
- }
- string += " ]";
- return string;
- }
- }
- public class Main {
- public static void main(String[] args) throws IOException {
- var scanner = new Scanner(System.in);
- var br = new BufferedReader(new InputStreamReader(System.in));
- int n = scanner.nextInt();
- var phones = new Phone[n];
- for (int i = 0; i < n; i++) {
- String name = br.readLine();
- phones[i] = new Phone(name);
- }
- int a = scanner.nextInt();
- var random = new Random();
- for (int i = 0; i < a; i++) {
- int x, y;
- do {
- x = Math.abs(random.nextInt() % n);
- y = Math.abs(random.nextInt() % n);
- } while (x == y);
- phones[x].call(phones[y]);
- }
- String name = br.readLine();
- for (int i = 0; i < n; i++) {
- System.out.println(phones[i] + "\n" + phones[i].nrOfCalls(name) + "\n");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement