Java复习41(PTA)

jmu-Java-m06 根据姓名以及电话号码查找联系人

分数 20

全屏浏览

切换布局

作者 郑如滨

单位 集美大学

该程序包含:

Person类:

属性:int id, String name, String phoneNumber,String address。

方法:

无参构造方法,有参构造方法(id, name, phoneNumber,addres),

各个参数的getter/setter方法,

toString()方法。

Utils类:

用于生成通讯录(具体代码下面已给出)

Main类的main方法:

使用Utils类创建一个通讯录personList,使用Map将personList中的person对象按name分类存储。即,key为person的name值,value为对应的person对象的List列表(说明:就是将相同name值的person对象放在一起List列表中)。

接下来就可以在该Map中根据姓名与电话号码查询联系人。查询方法如下:

  1. 输入n
  2. 输入n行的name以及phoneNumber值,以空格分开。然后根据name查找map,如果找到且在列表中的存在某个person对象的电话号码也相同,则输该person对象。如果找不到(即找不到name与电话号码均匹配的Person对象)则输出提示信息。
  3. 最后一行输出map的数量,即map.size()(该行输出在输出样例中未显示)

Utils类实现如下:

class Utils{

    private static final Random rd = new Random(3);
    private static final int number = 200000;
    private static List<String> getPersonName(){
        StringBuilder sb = new StringBuilder();
        List<String> names = new ArrayList<String>();
        for(int i=0;i<Utils.number;i++) {
            sb.delete(0, sb.length());
            for(int j=0;j<4;j++) {
                if(j==0) {
                    sb.append((char)('A'+rd.nextInt(26)));
                }
                else {
                    sb.append((char)('a'+rd.nextInt(26)));
                }
            }
            names.add(sb.toString());
        }
        return names;

    }
    private static List<String> getPhoneNumber(){
    
        String[] telFirst="134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153".split(",");
        StringBuilder sb = new StringBuilder();
        List<String> phoneNumbers = new ArrayList<String>();
        for(int i=0;i<Utils.number;i++) {
            sb.delete(0, sb.length());
            for(int j=0;j<9;j++) {
                if(j==0) {
                    sb.append(telFirst[rd.nextInt(telFirst.length)]);
                }
                else {
                    sb.append(rd.nextInt(10));
                }
            }
            phoneNumbers.add(sb.toString());
        }
        return phoneNumbers;
    }

    private static List<String> getPersonAddress(){
        List<String> addresses = new ArrayList<String>();
        String[] roads = {"HuanDao Road","WenZeng Road","ZhongShang Road","KaiHe Road","NanHua Road","SongBai Road","BinShui Road","ShiGu Road","JiYuan Road","YinJiang Road"};
        StringBuilder sb = new StringBuilder();
        for(int i=0;i<Utils.number;i++) {
            sb.delete(0, sb.length());
            for(int j=0;j<2;j++) {
                if(j==1) {
                    sb.append(roads[rd.nextInt(roads.length)]);
                }
                else {
                    sb.append("Room "+rd.nextInt(100)+"-"+rd.nextInt(1000)+"-"+rd.nextInt(10000)+", ");
                }
            }
            addresses.add(sb.toString());
        }
        return addresses;
    }

    public static List<Person> getPersonList(){
        List<String> names = Utils.getPersonName();
        List<String> phones = Utils.getPhoneNumber();
        List<String> addresses = Utils.getPersonAddress();
        List<Person> persons = new ArrayList<Person>();

        for(int i=0;i<Utils.number;i++) {
            Person person = new Person();
            person.setId(i);
            person.setName(names.get(i));
            person.setPhoneNumber(phones.get(i));
            person.setAddress(addresses.get(i));
            persons.add(person);
        }
        return persons;
    }
}

输入格式:

一个正整数n

接下去的n行:

输入name以及phoneNumber

输出格式:

如果找到对象则输出该对象

未找到对象则输出"Not found!"。

输入样例:

5
Vern 15678785276
Paul 13369862612
Todd 15094198414
Mike 15079239680
Tony 13739215033

输出样例:

Not found!
Person [id=162538, name=Paul, phoneNumber=13369862612, address=Room 8-446-6866, ShiGu Road]
Person [id=139814, name=Todd, phoneNumber=15094198414, address=Room 8-856-5546, JiYuan Road]
Not found!
Person [id=184950, name=Tony, phoneNumber=13739215033, address=Room 71-896-9345, SongBai Road]
161999

代码长度限制

50 KB

时间限制

2500 ms

内存限制

256 MB

栈限制

8192 KB

java 复制代码
import java.util.*;

public class Main {
    public static void main(String[] args) {
        List<Person> personList = Utils.getPersonList();
        Map<String, List<Person>> personMap = new HashMap<>();

        // 按姓名分类存储
        for (Person person : personList) {
            personMap.computeIfAbsent(person.getName(), k -> new ArrayList<>()).add(person);
        }

        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        scanner.nextLine(); // 读取换行符

        for (int i = 0; i < n; i++) {
            String line = scanner.nextLine();
            String[] parts = line.split(" ");
            String name = parts[0];
            String phoneNumber = parts[1];

            if (personMap.containsKey(name)) {
                List<Person> persons = personMap.get(name);
                boolean found = false;
                for (Person person : persons) {
                    if (person.getPhoneNumber().equals(phoneNumber)) {
                        System.out.println(person);
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    System.out.println("Not found!");
                }
            } else {
                System.out.println("Not found!");
            }
        }

        System.out.println(personMap.size()); // 输出map的数量
        scanner.close();
    }
}

class Person {
    private int id;
    private String name;
    private String phoneNumber;
    private String address;

    public Person() {
    }

    public Person(int id, String name, String phoneNumber, String address) {
        this.id = id;
        this.name = name;
        this.phoneNumber = phoneNumber;
        this.address = address;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Person [id=" + id + ", name=" + name + ", phoneNumber=" + phoneNumber + ", address=" + address + "]";
    }
}

class Utils {

    private static final Random rd = new Random(3);
    private static final int number = 200000;

    private static List<String> getPersonName() {
        StringBuilder sb = new StringBuilder();
        List<String> names = new ArrayList<String>();
        for (int i = 0; i < Utils.number; i++) {
            sb.delete(0, sb.length());
            for (int j = 0; j < 4; j++) {
                if (j == 0) {
                    sb.append((char) ('A' + rd.nextInt(26)));
                } else {
                    sb.append((char) ('a' + rd.nextInt(26)));
                }
            }
            names.add(sb.toString());
        }
        return names;

    }

    private static List<String> getPhoneNumber() {

        String[] telFirst = "134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153".split(",");
        StringBuilder sb = new StringBuilder();
        List<String> phoneNumbers = new ArrayList<String>();
        for (int i = 0; i < Utils.number; i++) {
            sb.delete(0, sb.length());
            for (int j = 0; j < 9; j++) {
                if (j == 0) {
                    sb.append(telFirst[rd.nextInt(telFirst.length)]);
                } else {
                    sb.append(rd.nextInt(10));
                }
            }
            phoneNumbers.add(sb.toString());
        }
        return phoneNumbers;
    }

    private static List<String> getPersonAddress() {
        List<String> addresses = new ArrayList<String>();
        String[] roads = {"HuanDao Road", "WenZeng Road", "ZhongShang Road", "KaiHe Road", "NanHua Road", "SongBai Road", "BinShui Road", "ShiGu Road", "JiYuan Road", "YinJiang Road"};
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < Utils.number; i++) {
            sb.delete(0, sb.length());
            for (int j = 0; j < 2; j++) {
                if (j == 1) {
                    sb.append(roads[rd.nextInt(roads.length)]);
                } else {
                    sb.append("Room " + rd.nextInt(100) + "-" + rd.nextInt(1000) + "-" + rd.nextInt(10000) + ", ");
                }
            }
            addresses.add(sb.toString());
        }
        return addresses;
    }

    public static List<Person> getPersonList() {
        List<String> names = Utils.getPersonName();
        List<String> phones = Utils.getPhoneNumber();
        List<String> addresses = Utils.getPersonAddress();
        List<Person> persons = new ArrayList<Person>();

        for (int i = 0; i < Utils.number; i++) {
            Person person = new Person();
            person.setId(i);
            person.setName(names.get(i));
            person.setPhoneNumber(phones.get(i));
            person.setAddress(addresses.get(i));
            persons.add(person);
        }
        return persons;
    }
}
相关推荐
禁默13 分钟前
深入浅出:AWT的基本组件及其应用
java·开发语言·界面编程
Cachel wood20 分钟前
python round四舍五入和decimal库精确四舍五入
java·linux·前端·数据库·vue.js·python·前端框架
Code哈哈笑22 分钟前
【Java 学习】深度剖析Java多态:从向上转型到向下转型,解锁动态绑定的奥秘,让代码更优雅灵活
java·开发语言·学习
gb421528725 分钟前
springboot中Jackson库和jsonpath库的区别和联系。
java·spring boot·后端
程序猿进阶26 分钟前
深入解析 Spring WebFlux:原理与应用
java·开发语言·后端·spring·面试·架构·springboot
zfoo-framework33 分钟前
【jenkins插件】
java
风_流沙39 分钟前
java 对ElasticSearch数据库操作封装工具类(对你是否适用嘞)
java·数据库·elasticsearch
ProtonBase1 小时前
如何从 0 到 1 ,打造全新一代分布式数据架构
java·网络·数据库·数据仓库·分布式·云原生·架构
乐之者v1 小时前
leetCode43.字符串相乘
java·数据结构·算法