携程笔试三道编程题

第一题(AC)电话接听最少人员
public class Main {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		scanner.nextLine();

		int[] arr = new int[n];
		int[] dep = new int[n];
		for (int i = 0; i < n; i++) {
			String line = scanner.nextLine();
			String[] sa = line.split(",");
			arr[i] = Integer.parseInt(sa[0]);
			dep[i] = Integer.parseInt(sa[1]);
		}

		int result = find(arr, dep, n);

		System.out.println(result);
		scanner.close();
	}
	
	static int find(int arr[], int dep[], int n) {
		Arrays.sort(arr);
		Arrays.sort(dep);

		int result = 0;
		int need = 1;

		int i = 1;
		int j = 0;

		while (i < arr.length && j < dep.length) {
			if(arr[i] < dep[j]) {
				need += 1;
				i++;
				if(need > result) {
					result = need;
				}
			} else {
				need -= 1;
				j++;
			}
		}

		return result;
	}

}

第二题(AC88%)海豚数量
public class Main {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		scanner.nextLine();

		int m = scanner.nextInt(); // life time
		scanner.nextLine();
		
		int p = scanner.nextInt();
		scanner.nextLine();
		
		int[] birth = new int[p];
		for(int i=0; i<p;i++) {
			birth[i] = scanner.nextInt();
			scanner.nextLine();
		}
		
		int x = scanner.nextInt();
		
		int[] ages = new int[m+1];
		ages[0] = 1;
		
		for(int k=0; k<x; k++) {
			for(int i=m; i>0; i--) {
				ages[i] = ages[i-1];
			}
			ages[0] = 0;
			for(int bir : birth) {
				ages[0] = ages[0] + ages[bir];
			}
		}
		
		int res = 0;
		for(int i=0; i<=m; i++) {
			res += ages[i];
		}
		res = res * n;

		System.out.println(res);
		scanner.close();
	}
	
}

第三题(AC)字符串校正
public class Main {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		String[] corrects = {"surprise", "happy", "ctrip", "travel", "wellcome","student","system","program","editor"};
		
		while(scanner.hasNextLine()) {
			String word = scanner.nextLine();
			if(word == null || word.length() == 0) {
				break;
			}
			
			boolean hasWord = false;
			for(String correct : corrects) {
				if(correctFunc(word, correct)) {
					System.out.println(correct);
					hasWord = true;
					break;
				}
			}
			
			if(!hasWord) {
				System.out.println("null");
			}
		}
		
		scanner.close();
	}

	private static boolean correctFunc(String word, String correct) {
		int line = word.length();
		int col = correct.length();
		
		int[][] dp = new int[line][col];
		dp[0][0] = word.charAt(0) == correct.charAt(0) ? 0 : 1;
		String word0 = String.valueOf(word.charAt(0));
		for(int i=1; i<col; i++) {
			if(word.charAt(0) == correct.charAt(i)) {
				if(correct.substring(0, i).contains(word0)) {
					dp[0][i] = dp[0][i-1] + 1;
				} else {
					dp[0][i] = dp[0][i-1];
				}
			} else {
				dp[0][i] = dp[0][i-1] + 1;
			}
		}
		String correct0 = String.valueOf(correct.charAt(0));
		for(int i=1; i<line; i++) {
			if(correct.charAt(0) == word.charAt(i)) {
				if(word.substring(0, i).contains(correct0)) {
					dp[i][0] = dp[i-1][0] + 1;
				} else {
					dp[i][0] = dp[i-1][0];
				}
			} else {
				dp[i][0] = dp[i-1][0] + 1;
			}
		}
		
		for(int i=1; i<line; i++) {
			for(int j=1; j<col; j++) {
				int tmp = Integer.MAX_VALUE;
				if(word.charAt(i) == correct.charAt(j)) {
					tmp = dp[i-1][j-1];
				} else {
					tmp = dp[i-1][j-1] + 1;
				}
				dp[i][j] = Math.min(tmp, Math.min(dp[i-1][j] + 1, dp[i][j-1] + 1));
			}
		}
		
		if(dp[line-1][col-1] <= 2) {
			return true;
		}
		
		return false;
	}

}



#携程##笔试题目#
全部评论
楼主第二题题目的birth数组是干嘛的?没太看懂?能说明一下吗
1 回复
分享
发布于 2020-04-01 22:05
联想
校招火热招聘中
官网直投
大佬tql
点赞 回复
分享
发布于 2020-04-01 21:01
强啊
点赞 回复
分享
发布于 2020-04-01 21:01
太强了,第二道没想出来a了25骗分
点赞 回复
分享
发布于 2020-04-01 21:02
tql,我第二题也被tm  int ac88卡半天 ,最后应该是long就好了但没来得及改 -
点赞 回复
分享
发布于 2020-04-01 21:03
tql,同小海豚卡住了┭┮﹏┭┮
点赞 回复
分享
发布于 2020-04-01 21:08
大佬,可以说下题解思路吗
点赞 回复
分享
发布于 2020-04-01 21:09
大佬强
点赞 回复
分享
发布于 2020-04-01 21:11
前排膜大佬
点赞 回复
分享
发布于 2020-04-01 21:13
第一题有LC类似的题吗?
点赞 回复
分享
发布于 2020-04-01 21:14
大佬
点赞 回复
分享
发布于 2020-04-01 21:14
跟大佬一比感觉自己菜的抠脚
点赞 回复
分享
发布于 2020-04-01 21:16
第二题他的birth是指海豚在那一个岁数的时候会生孩子吗😂
点赞 回复
分享
发布于 2020-04-01 21:26
大佬,能讲讲第一题的思路吗?这是哪种题型啊
点赞 回复
分享
发布于 2020-04-01 21:27
大佬太强了
点赞 回复
分享
发布于 2020-04-01 21:28
太强了
点赞 回复
分享
发布于 2020-04-01 21:39
第二题楼主思路肯定没什么问题,超时的话把递推形式改成矩阵乘法弄个快速幂试试?long如果溢出的话改成BigInteger试试?我同学做完和我讨论的我自己没做不知道数据范围,反正我也不知道行不行是我也是个菜比
点赞 回复
分享
发布于 2020-04-01 21:55
有没有完整题目
点赞 回复
分享
发布于 2020-04-01 22:17
请问这个是什么岗位的题
点赞 回复
分享
发布于 2020-04-01 22:30

相关推荐

头像
04-09 14:29
Java
点赞 评论 收藏
转发
19 54 评论
分享
牛客网
牛客企业服务