题目如图:
第一题:
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");}}
第二题面试官未作评价,尚未知结果如何。