Este problema pede para que, a partir de uma lista telefônica, seja inserido um número e retorne a mesma quantidade de contatos relativo à esse número.
O código começa assim:
//Complete this code or write your own from scratch import java.util.*; import java.io.*; class Solution{ public static void main(String []argh) { Scanner in = new Scanner(System.in); int n=in.nextInt(); in.nextLine(); for(int i=0;i<n;i++) { String name=in.nextLine(); int phone=in.nextInt(); in.nextLine(); } while(in.hasNext()) { String s=in.nextLine(); } } }
=========
O resultado final é:
package com.possible.map; import java.util.*; import java.io.*; public class Solution { public static void main(String[] args) throws FileNotFoundException { Scanner in = new Scanner(new File("input.txt")); int n = in.nextInt(); // quantidade de contatos que vc quer in.nextLine(); HashMap<String,Integer> listaTelefonica = new HashMap<>(); for (int i = 0; i < n; i++) { String name = in.nextLine(); // pega o nome int phone = in.nextInt(); // pega o telefone listaTelefonica.put(name, phone); in.nextLine(); } while (in.hasNext()) { String s = in.nextLine(); if(listaTelefonica.containsKey(s)) // se existir System.out.println(s + "=" + listaTelefonica.get(s)); else System.out.println("Not found"); } } }
=========
Top comments (0)