考试时间:2h
题型:4道编程题
很变态的是,考试的时候需要始终开着手机端微信中的一个小程序来防止作弊。
本人很菜,这些思路都是借鉴了广大网友的。
直接上题吧
input
1 3 8 7 10
2 1 7 5 9 8
output
1 3 8 9 10
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Scanner;
/**
* 给定两个数组A和B,其中数组A是几乎严格升序排列的,几乎的定义是只需改变其中一个数,即可满足完全升序排列。你的任务是从A中找到这个数组,并从数组B中选取一个数将其代替,
* 使得A是严格升序排列的,请找出B中满足要求的最大数字,并输出有序数组,如不存在则输出NO。
*/
public class Q1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
// 输入
String[] aa = in.nextLine().split(" ");
String[] bb = in.nextLine().split(" ");
int[] a = new int[aa.length];
int[] b = new int[bb.length];
for (int i = 0; i < a.length; i++) {
a[i] = Integer.parseInt(aa[i]);
}
for (int i = 0; i < b.length; i++) {
b[i] = Integer.parseInt(bb[i]);
}
printSorted(a, b);
}
}
public static void printSorted(int[] a, int[] b){
int i = 0;
for (;i=a[i+1]){
break;
}
}
Arrays.sort(b);
i = i+1;
int maxVal = Integer.MAX_VALUE;
if(i+1 0){
minVal = a[i-1];
}
v = binarySearch(b,0,b.length-1,minVal,a[i+1]);
}
if(v==-1){
System.out.println("NO");
return;
}
a[i] = v;
for(int j=0;jr)
return -1;
if(midValue<=v1){
return binarySearch(arr, mid+1,r,v1,v2);
}else if(midValue>=v2){
return binarySearch(arr,l,mid-1,v1,v2);
}else{
int tmp = mid+1;
while (tmp<=r && arr[tmp]
input
CAT TEAM MAC
output
true
牛客上有人说,只要首,尾的字母是偶数就可以通过。可能是测试数据太弱了。但实际上AB AB这一类是不能成环的。
public class E2 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()) {
String[] words = in.nextLine().split(" ");
if(null == words || words.length < 2) System.out.println("false");
boolean[] isUsed = new boolean[words.length];
boolean flag = backtrack(words, new ArrayList(), isUsed);
if(flag) System.out.println("true");
else System.out.println("false");
}
}
private static boolean backtrack(String[] words, ArrayList curList, boolean[] isUsed) {
boolean flag = false;
if(words.length == curList.size()) {
String first = curList.get(0);
String last = curList.get(curList.size()-1);
return last.charAt(last.length()-1) == first.charAt(0);
}
for(int i = 0; i < words.length; ++i) {
if(isUsed[i]) continue;
if(curList.size() == 0) {
curList.add(words[i]);
} else {
String prev = curList.get(curList.size()-1);
if(prev.charAt(prev.length()-1) != words[i].charAt(0)) continue;
curList.add(words[i]);
}
isUsed[i] = true;
flag = backtrack(words, curList, isUsed);
isUsed[i] = false;
curList.remove(curList.size()-1);
if(flag) break;
}
return flag;
}
}
这题值AC了95,不知道问题出在哪里。
import java.util.*;
public class Q3 {
static class Task {
int seq;
int weight;
/**
* @param n 任务的编号
* @param w 任务的花费时间
*/
public Task(int n, int w) {
seq = n;
weight = w;
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
Task[] t = new Task[n+1];
for(int i = 1; i < n+1; ++i) {
t[i] = new Task(i, in.nextInt());
}
// construct dependency raph
Map> graph = new HashMap<>();
int[] indegree = new int[n+1];
for(int i = 0; i < m; ++i) {
int u = in.nextInt();
int v = in.nextInt();
if(graph.containsKey(u)) {
graph.get(u).add(v);
} else {
List edges = new ArrayList<>();
edges.add(v);
graph.put(u, edges);
}
indegree[v]++;
}
//topological sort, 按照任务的时间,从小到大排序
PriorityQueue queue = new PriorityQueue<>(new Comparator() {
@Override
public int compare(Task o1, Task o2) {
return o1.weight-o2.weight;
}
});
// 将入度为0,即没有依赖关系的,根据贪心策略,按时间从短到长依次执行
for(int i = 1; i < n+1; ++i) {
if(indegree[i] == 0) queue.offer(t[i]);
}
// 将入度为0的任务完成后,其后续任务可以将入度-1
List res = new ArrayList<>();
while (! queue.isEmpty()) {
Task complete = queue.poll();
res.add(complete.seq);
if (graph.containsKey(complete.seq)) {
for (int i: graph.get(complete.seq)) {
if (--indegree[i] == 0) {
queue.offer(t[i]);
}
}
}
}
for (int i = 0; i < n; i++) {
System.out.println(res.get(i));
if (i != n - 1) System.out.println(" ");
}
}
}
import java.util.Arrays;
import java.util.Comparator;
import java.util.Map;
import java.util.Scanner;
public class Q4 {
public static class Box {
int len;
int weight;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
Box[] b = new Box[n+1];
b[0] = new Box();
for(int i = 1; i < n+1; ++i) {
b[i] = new Box();
b[i].len = in.nextInt();
}
for(int i = 1; i < n+1; ++i) {
b[i].weight = in.nextInt();
}
Arrays.sort(b, new Comparator() {
@Override
public int compare(Box o1, Box o2) {
return o1.len == o2.len ? o1.weight - o2.weight : o1.len - o2.len;
}
});
int maxRes = 0;
int[][] dp = new int[n+1][n+1];
for (int i = 0; i < n+1; i++) {
for (int j = 0; j < n+1; j++) {
dp[i][j] = Integer.MAX_VALUE;
}
}
dp[0][0] = 0;
for (int i = 1; i < n+1; i++) {
for (int k = 0; k < i; k++) {
if (b[k].len < b[i].len) { // 上层比下层要小
for (int j = 0; j <= k; j++) {
if (dp[k][j] != Integer.MAX_VALUE && dp[k][j] < b[i].weight * 7) {
dp[i][j+1] = Math.min(dp[i][j+1], dp[k][j] + b[i].weight);
maxRes = Math.max(maxRes, j + 1);
}
}
}
}
}
System.out.println(maxRes);
}
}
参考资料:
https://www.hytheory.com/algorithm/pdd-exam/