当前位置: 首页 > article >正文

面试编程题目(一)细菌总数计算

题目如图
在这里插入图片描述
第一题

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @description: 细菌实体类
 * @author: zhangmy
 * @Version: 1.0
 * @create: 2021-03-30 11:23
 **/
@Data
@AllArgsConstructor
public class Bacteria {

    //最大繁殖次数
    private static final int MAX_BREED_NUMBER = 3;
    //可繁殖时间
    private static final int CAN_BREED_TIME = 20;
    //繁殖间隔
    private static final int BREED_CD_TIME = 30;

    //存活时间
    private int liveMinute;
    //繁殖次数
    private int breedNumber;
    //距离可繁殖的倒计时
    private int breedCountDown;
    //当前是否可繁殖
    private boolean isCanBreed;
    //细菌是否死亡
    private boolean isDead;

    public Bacteria() {
        this.liveMinute = 0;
        this.breedNumber = 0;
        this.isCanBreed = false;
        this.breedCountDown = 20;
        this.isDead = false;
    }

    //生长
    public void grow(int minutes) {
        this.liveMinute = this.liveMinute + minutes;
        this.breedCountDown = this.breedCountDown - minutes;
        if (this.liveMinute > 150) {
            this.isDead = true;
            return;
        }
        if (!this.isDead && this.breedCountDown == 0 && this.breedNumber < MAX_BREED_NUMBER) {
            this.isCanBreed = true;
        } else {
            this.isCanBreed = false;
        }
    }

    //繁殖
    public List<Bacteria> breed(){
        if (!this.isCanBreed) {
            return Collections.emptyList();
        }
        //繁殖过后次数加1
        this.breedNumber++;
        this.breedCountDown = BREED_CD_TIME;
        return Arrays.asList(new Bacteria(),new Bacteria());
    }

}

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

/**
 * @description: 测试Main
 * @author: zhangmy
 * @Version: 1.0
 * @create: 2021-03-30 11:59
 **/
public class Main {

    public static void main(String[] args) {

        Bacteria bacteria1 = new Bacteria();
        System.out.println(List.class.getClassLoader());
        System.out.println(bacteria1.getClass().getClassLoader().getParent());
        Scanner in = new Scanner(System.in);
        while (true) {
            System.out.println("输入时间:");
            int minute = in.nextInt();
            long totalStart = System.currentTimeMillis();
            List<Bacteria> bacterias = new ArrayList<>();
            bacterias.add(new Bacteria());
            for (int i = 10; i <= minute; i+=10) {
                List<Bacteria> bornBacterias = new ArrayList<>();
                long start = System.currentTimeMillis();
                for (int j = 0; j < bacterias.size();j++) {
                    Bacteria bacteria = bacterias.get(j);
                    bacteria.grow(10);
                    if (bacteria.isDead()) {
                        bacterias.remove(bacteria);
                        j--;
                        continue;
                    }
                    bornBacterias.addAll(bacteria.breed());
                }
                long end = System.currentTimeMillis();
                System.out.println("遍历了" + bacterias.size() + "个!耗时:" + (end - start) + "ms");
                bacterias.addAll(bornBacterias);

                //System.out.println("观察时间点:" + i + ",细菌有:" + bacterias.size());
            }
            long totalEnd = System.currentTimeMillis();
            System.out.println("总耗时:" + (totalEnd - totalStart) + "ms");
            System.out.println("细菌总个数:" + bacterias.size());
        }
    }

}

第一题面试官给的答案是满意。

第二题

import java.util.*;
import java.util.stream.Collectors;

/**
 * @description: 数字区间
 * @author: zhangmy
 * @Version: 1.0
 * @create: 2021-03-30 13:54
 **/
public class ParseNumber {

    //解析输入
    public static Set<Long> parseRule(String rule) {
        Set<Long> result = new HashSet<>();
        String[] split = rule.split(",");
        for (int i = 0; i < split.length; i++) {
            if (split[i].contains("-")) {
                String[] numbers = split[i].split("-");
                if (numbers.length == 2) {
                    Long start = Long.parseLong(numbers[0]);
                    Long end = Long.parseLong(numbers[1]);
                    for (Long index = start; index < end; index++) {
                        result.add(index);
                    }
                }
            } else {
                result.add(Long.parseLong(split[i]));
            }
        }
        return result;
    }

    public static String deparseRule(Set<Long> set) {
        StringBuilder result = new StringBuilder();
        List<String> members = new ArrayList<>();
        List<Long> list = new ArrayList<>(set).stream().sorted().collect(Collectors.toList());

        for (int i = 0; i < list.size(); i++) {
            StringBuilder sb = new StringBuilder();
            sb.append(list.get(i));
            //区间形式
            if ((i + 1 < list.size()) && (list.get(i + 1) == list.get(i) + 1)) {
                sb.append("-");
                while ((i + 1 < list.size()) && (list.get(i + 1) == list.get(i) + 1)) {
                    i++;
                }
                //右开,需要+1
                sb.append(list.get(i) + 1);
            }
            members.add(sb.toString());
        }
        for (String temp : members) {
            result.append(temp).append(",");
        }
        //去掉最后一个逗号
        String substring = result.substring(0, result.length() - 1);
        return substring;
    }

    //判断是否是子集
    public static boolean isFullContains(Set<Long> a,Set<Long> b) {
        for (Long i : a) {
            if (!b.contains(i)) {
                return false;
            }
        }
        return true;
    }

    //计算交集
    public static Set<Long> calIntersection(Set<Long> a,Set<Long> b) {
        Set<Long> result = new HashSet<>();
        for (Long i : a) {
            if (b.contains(i)) {
                result.add(i);
            }
        }
        return result;
    }

    //计算并集
    public static Set<Long> calSet(Set<Long> a,Set<Long> b) {
        a.addAll(b);
        return a;
    }

}

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

/**
 * @description: 测试Main
 * @author: zhangmy
 * @Version: 1.0
 * @create: 2021-03-30 11:59
 **/
public class Main {

    public static void main(String[] args) {
        //默认区间左闭右开
        String as = "1,5-99999";
        String bs = "2,4-99999";

        long start = System.currentTimeMillis();
        //判断是否包含
        Set<Long> a = ParseNumber.parseRule(as);
        Set<Long> b = ParseNumber.parseRule(bs);
        System.out.println("是否包含:" + ParseNumber.isFullContains(a,b));
        //求交集
        Set<Long> intersection = ParseNumber.calIntersection(a, b);
        System.out.println("交集:" + ParseNumber.deparseRule(intersection));

        //求并集
        Set<Long> set = ParseNumber.calSet(a, b);
        System.out.println("并集:" + ParseNumber.deparseRule(set));
        long end = System.currentTimeMillis();
        System.out.println("消耗时间:" + (end - start) + "ms");
    }

}

第二题面试官未作评价,尚未知结果如何。


http://www.kler.cn/a/392708.html

相关文章:

  • 文件夹被占用了无法删除怎么办?强制粉碎文件夹你可以这样操作
  • Android音频架构
  • 物理设备命名规则(Linux网络服务器 15)
  • 词嵌入方法(Word Embedding)
  • 机器学习——损失函数、代价函数、KL散度
  • 谷歌浏览器的自动翻译功能如何开启
  • Mybatis-plus 使用分页插件
  • 重生之从零设计 MySQL 架构
  • cuda的3DArray和TextureObject
  • PHP搭建开发环境(Windows系统)
  • 代码随想录算法训练营第二十九天| 134. 加油站 、135. 分发糖果 、860.柠檬水找零、406.根据身高重建队列。c++转java
  • 本地权限提升漏洞分析
  • Bootstrap 5 轮播
  • Proteus中数码管动态扫描显示不全(已解决)
  • 微积分复习笔记 Calculus Volume 1 - 5.3 The Fundamental Theorem of Calculus
  • 【ChatGPT】通过Prompt技巧优化ChatGPT的营销文案输出
  • 【优选算法篇】化繁为简,见素抱朴:从乱象中重构秩序的艺术
  • 用于在 .NET 中构建 Web API 的 FastEndpoints 入门
  • python私有化get和set的使用
  • 【实用教程】使用思维导图增强 JavaScript甘特图项目工作流程的可见性
  • 如何制作代购系统的用户管理模块
  • 免费白嫖:数据分析常用软件安装视频
  • http(s)接口设计注意事项
  • 【大数据学习 | HBASE】hbase的读数据流程与hbase读取数据
  • 下载并安装Cmake3.29.5 windows安装包
  • HTTP常见的请求头有哪些?都有什么作用?在 Web 应用中使用这些请求头?