记录三道编程题
第一题
求最小客服人数 时间限制:C/C++语言 1000MS;其他语言 3000MS 内存限制:C/C++语言 65536KB;其他语言 589824KB 题目描述: 携程呼叫中心7×24小时帮助旅客解决在途中的各种问题,为了尽可能提升服务质量,公司希望客服人数可以满足所有旅客的来电,不用排队等待人工客服。现在提供客服中心所有的通话记录时间,你能算出最少需要多少名客服吗? 输入 输入一个n表示要输入的通话记录个数,接下来输入n行,每行为逗号相隔的两个整数,两个数字分别代表呼入时间和挂断时间的时间戳。 举例:10,30,表示[10,30),代表第10秒呼入,第30秒已经挂断,即第30秒可以接入新的来电; 每一行都是一条通话记录,通话记录已经按呼入时间由小到大排序; 输出 输出一个整数; 代表最少需要多少客服,可以满足所有旅客来电不用等待。 样例输入 6 0,30 0,50 10,20 15,30 20,50 20,65 样例输出 5
第一次,ac:20%,用错了方法,考虑不周,浪费40多分钟,导致最后一题没时间做
import java.util.ArrayList;
import java.util.Scanner;
public class First {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
ArrayList<Person> list = new ArrayList<Person>();
//输入n行数据
sc.nextLine();
for(int i = 0; i < n; i++){
String string = sc.nextLine();
String[] s = string.split(",");
Integer[] arr = new Integer[2];
arr[0] = Integer.parseInt(s[0]);
arr[1] = Integer.parseInt(s[1]);
if(list.size() == 0){
Person person1 = new Person();
person1.time.add(arr);
list.add(person1);
} else {
//判断每个客服这个时间段是否有时间
for(int j = 0; j < list.size(); j++){
if(judge(list.get(j), arr)){
list.get(j).time.add(arr);
} else {
Person person2 = new Person();
person2.time.add(arr);
list.add(person2);
}
}
}
}
//输出客服人数
System.out.println(list.size());
}
//判断该客服这段时间是否有空
public static boolean judge(Person person, Integer[] arr){
Integer[] temp;
for(int i = 0; i < person.time.size(); i++){
temp = person.time.get(i);
//客服时间段包含该时间段
if(temp[0] <= arr[0] && temp[1] >= arr[1]){
return false;
}
//相交
if((temp[0] >= arr[0] && temp[0] < arr[1]) || (temp[1] > arr[0] && temp[1] <= arr[1])){
return false;
}
}
return true;
}
}
class Person{
ArrayList<Integer[]> time = new ArrayList<Integer[]>();
}
第二次:ac:100% 方法:bitmap
import java.util.HashMap;
import java.util.Scanner;
public class First2 {
public static void main(String[] args) {
int count = 0;
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
HashMap<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < n; i++){
String string = sc.nextLine();
String[] split = string.split(",");
int[] arr = new int[2];
arr[0] = Integer.parseInt(split[0]);
arr[1] = Integer.parseInt(split[1]);
for(int j = arr[0];j < arr[1]; j++){
if(map.get(j) == null){
map.put(j,1);
} else {
map.put(j, map.get(j) + 1);
}
count = map.get(j) > count ? map.get(j) : count;
}
}
System.out.println(count);
}
}
第二题
携程海洋馆的海豚小宝宝 时间限制:C/C++语言 1000MS;其他语言 3000MS 内存限制:C/C++语言 65536KB;其他语言 589824KB 题目描述: 携程海洋馆中有 n 只萌萌的小海豚,初始均为 0 岁,每只小海豚的寿命是 m 岁, 且这些小海豚会在 birthYear[i] 这些年份生产出一位宝宝海豚(1 <= birthYear[i] <= m),每位宝宝海豚刚出生为 0 岁。 问 x 年时,携程海洋馆有多少只小海豚? 输入 n(初始海豚数) m(海豚寿命) 海豚生宝宝的年份数量(假设为p) 海豚生宝宝的年份1 ... 海豚生宝宝的年份p x(几年后) 输出 x年后,共有多少只小海豚 样例输入 5 5 2 2 4 5 样例输出 20
ac:38%
import java.util.HashMap;
import java.util.Scanner;
public class Second {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int p = sc.nextInt();
int[] arr = new int[p];
for(int i = 0; i < p; i++){
arr[i] = sc.nextInt();
}
int x = sc.nextInt();
HashMap<Integer, Integer> map = new HashMap<>();
map.put(1,n);
int temp = 0;
for(int i = 1; i <= x; i++){
if(map.get(i) != null) {
temp = map.get(i);
for (int j = 0; j < arr.length; j++) {
if (map.get(i + arr[j]) == null) {
map.put(i + arr[j], temp);
} else {
map.put(i + arr[j], map.get(i + arr[j]) + temp);
}
}
}
}
int count = 0;
for(int i = x - m + 1; i <= x; i++){
if(map.get(i) != null){
count += map.get(i);
}
}
System.out.println(count);
}
}
第三题
模拟ElasticSearch中的FuzzyQuery中的单词纠正 时间限制:C/C++语言 1000MS;其他语言 3000MS 内存限制:C/C++语言 65536KB;其他语言 589824KB 题目描述: ElasticSearch 是常用的开源搜索引擎,支持fuzzyQuery 给搜索带来很大便利。 其简单原理如下,surprize有拼写错误,把z换成s后得到 surprise,即纠正一个字母, 就可以匹配正确的单词。 同样,把surprize的z替换成s,然后在末尾加个d,可以得到surprised。 给定字典[ "surprise", "happy", "ctrip", "travel", "wellcome","student","system","program","editor"] 为正确单词。 编程实现单词纠正,判断输入的单词能否在2(包含)次纠正操作内得到字典中的单词。 纠正操作是以下三种, 1:替换字符串中的一个字母; 2:删除字符串中的一个字母; 3:在字符串中增加一个字母。 输入 待纠正的单词1 ... 待纠正的单词n 输出 如果可以匹配,请返回字典中的单词, 如果无法匹配,请返回字符串null 样例输入 hipp 样例输出 happy
没时间做了