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

embedded/2024/11/18 13:42:28/

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

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();}//繁殖过后次数加1this.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++;}//右开,需要+1sb.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.ppmy.cn/embedded/138554.html

相关文章

《Django 5 By Example》阅读笔记:p76-p104

《Django 5 By Example》学习第4天&#xff0c;p76-p104总结&#xff0c;总计29页。 一、技术总结 1.环境变量管理 这里作者使用的是&#xff1a;python-decouple&#xff0c;本人在实际项目中使用的是python-dotenv&#xff0c;这里只是简单的使用&#xff0c;感觉两者差不…

[pyspark] pyspark中如何修改列名字

使用 .withColumnRenamed 来重命名&#xff0c;直接看demo&#xff1a; from pyspark.sql import SparkSessionspark SparkSession.builder.appName("example").getOrCreate()data [("Alice", 1, 200),("Bob", 2, 300),("Charlie",…

Python | Leetcode Python题解之第564题数组嵌套

题目&#xff1a; 题解&#xff1a; class Solution:def arrayNesting(self, nums: List[int]) -> int:ans, n 0, len(nums)for i in range(n):cnt 0while nums[i] < n:num nums[i]nums[i] ni numcnt 1ans max(ans, cnt)return ans

VAM本体整合包,本体人物卡

已更至2024年11月】全网人物卡最全&#xff01;所见即所得解压既玩。资源整合包较大&#xff0c;选择性下载想玩什么下什么&#xff01;&#xff01;&#xff01; 1.包含上千付费级精品场景&#xff0c;新增数位神佬合集&#xff0c;新增绝版素材。 2.没有场景是没有灵魂的&…

Java开发人员从了学习ArkTs笔记(三)-数据结构与线程通信全解析

大家好&#xff0c;我是一名热爱Java开发的开发人员。目前&#xff0c;我正在学习ARKTS&#xff08;Advanced Java Knowledge and Technology Stack&#xff09;&#xff0c;并将不断输出我的学习笔记。我将在这里分享我学习ARKTS的过程和心得&#xff0c;希望能够为其他开发人…

Scala学习记录,case class,迭代器

case class case class创建的对象的属性是不可改的 创建对象&#xff0c;可以不用写new 自动重写&#xff1a;toString, equals, hashCode, copy 自动重写方法&#xff1a;toString,equals,hashCode,copy 小习一下 1.case class 的定义语法是什么 基本形式&#xff1a;case …

数据结构王道P234第二题

#include<iostream> using namespace std; int visit[MAxsize]; int color[MaxSize];//1表示红&#xff0c;2表示白&#xff1b; bool dfs(Graph G, int i){visit[i]1;ArcNode *p;bool flag1;for(pG.vertices[i].firsrarc; p ; pp->next){int jp->adjvex;if(!visi…

Java-02 深入浅出 MyBatis - MyBatis 快速入门(无 Spring) POM Mapper 核心文件 增删改查

点一下关注吧&#xff01;&#xff01;&#xff01;非常感谢&#xff01;&#xff01;持续更新&#xff01;&#xff01;&#xff01; 大数据篇正在更新&#xff01;https://blog.csdn.net/w776341482/category_12713819.html 目前已经更新到了&#xff1a; MyBatis&#xff…