Skip to content

Commit 05812c8

Browse files
authored
Add files via upload
1 parent 136423f commit 05812c8

File tree

8 files changed

+288
-0
lines changed

8 files changed

+288
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package CampusRecruitment2019;
2+
3+
import java.util.Arrays;
4+
import java.util.Comparator;
5+
import java.util.HashMap;
6+
import java.util.Scanner;
7+
8+
// 牛牛找工作
9+
public class Main001 {
10+
public static void main(String[] args) {
11+
Scanner sc = new Scanner(System.in);
12+
13+
int n = sc.nextInt();
14+
int m = sc.nextInt();
15+
int[][] job = new int[n][2];
16+
int[] partner = new int[m];
17+
18+
for (int i = 0; i < n; i++) {
19+
job[i][0] = sc.nextInt();
20+
job[i][1] = sc.nextInt();
21+
}
22+
for (int i = 0; i < m; i++) {
23+
partner[i] = sc.nextInt();
24+
}
25+
26+
// 二维数组按照第一个元素升序排序,即对工作难度升序排序
27+
// Arrays.sort(job, (o1, o2) -> (o1[0] - o2[0])); // 写法一
28+
// Arrays.sort(job, Comparator.comparingInt(o -> o[0])); // 写法二
29+
Arrays.sort(job, new Comparator<int[]>() { // 写法三
30+
@Override
31+
public int compare(int[] o1, int[] o2) {
32+
return o1[0] - o2[0];
33+
}
34+
});
35+
// 对小伙伴能量值升序排序
36+
int[] partner2 = Arrays.copyOf(partner, m);
37+
Arrays.sort(partner2);
38+
39+
HashMap<Integer, Integer> map = new HashMap<>();
40+
int j = 0, power;
41+
for (int i = 0; i < m; i++) {
42+
power = partner2[i];
43+
// 能量值小于最低工作难度,报酬置0
44+
if (power < job[j][0]) {
45+
map.put(power, 0);
46+
continue;
47+
}
48+
// 不断提升能量值范围内的工作难度,并逐个将工作难度的报酬置为当前能量范围的最大报酬
49+
while (j + 1 < n && power >= job[j + 1][0]) {
50+
if (job[j][1] > job[j + 1][1])
51+
job[j + 1][1] = job[j][1];
52+
j++;
53+
}
54+
// 记录当前能量值可获得的最大报酬
55+
map.put(power, job[j][1]);
56+
}
57+
// 按照原能量值顺序输出报酬
58+
for (int i = 0; i < m; i++)
59+
System.out.println(map.get(partner[i]));
60+
}
61+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package CampusRecruitment2019;
2+
3+
import java.util.HashMap;
4+
import java.util.Scanner;
5+
6+
// X游戏
7+
public class Main041 {
8+
public static void main(String[] args) {
9+
Scanner sc = new Scanner(System.in);
10+
int n = sc.nextInt();
11+
int res = 0;
12+
for (int i = 1; i <= n; i++) {
13+
String str = String.valueOf(i);
14+
if ((str.contains("2") || str.contains("5") || str.contains("6") || str.contains("9")) && (!str.contains("3") && !str.contains("4") && !str.contains("7")))
15+
res++;
16+
}
17+
System.out.println(res);
18+
}
19+
}
20+
21+
/*
22+
public class Main041 {
23+
public static void main(String[] args) {
24+
Scanner sc = new Scanner(System.in);
25+
int n = sc.nextInt();
26+
27+
// 记录旋转规则
28+
HashMap<Character, Character> map = new HashMap<>();
29+
map.put('0', '0');
30+
map.put('1', '1');
31+
map.put('8', '8');
32+
map.put('2', '5');
33+
map.put('5', '2');
34+
map.put('6', '9');
35+
map.put('9', '6');
36+
int res = 0;
37+
Character newChar;
38+
for (int i = 1; i <= n; i++) {
39+
// 数字转化为字符数组
40+
String str = String.valueOf(i);
41+
char[] ch = str.toCharArray();
42+
// 判断是否全部为有效数字
43+
boolean flag = true;
44+
for (int j = 0; j < ch.length; j++) {
45+
newChar = map.get(ch[j]);
46+
// 获取并替换旧字符
47+
if (newChar != null) {
48+
ch[j] = newChar;
49+
} else {
50+
// 包含无效数字
51+
flag = false;
52+
break;
53+
}
54+
}
55+
// 旋转后数字与原来的不同
56+
if (flag && !String.valueOf(ch).equals(str))
57+
res += 1;
58+
}
59+
System.out.println(res);
60+
}
61+
}
62+
*/
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package CampusRecruitment2019;
2+
3+
import java.util.Scanner;
4+
5+
// 跳格子游戏
6+
public class Main042 {
7+
public static void main(String[] args) {
8+
Scanner sc = new Scanner(System.in);
9+
int n = sc.nextInt();
10+
11+
int a = 1, b = 1;
12+
for (int i = 0; i < n; i++) {
13+
int temp = a;
14+
a = b;
15+
b += temp;
16+
}
17+
System.out.println(a);
18+
}
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package CampusRecruitment2019;
2+
3+
import java.util.Scanner;
4+
5+
// 员工考勤记录
6+
public class Main044 {
7+
public static void main(String[] args) {
8+
Scanner sc = new Scanner(System.in);
9+
int n = sc.nextInt();
10+
11+
// 1种:全P
12+
// n种:含1个A
13+
// (n²-n)/2种:含2个A
14+
int res = 1 + n + (int)((Math.pow(n, 2) - n) / 2);
15+
System.out.println(res);
16+
}
17+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package CampusRecruitment2019;
2+
3+
import java.util.Scanner;
4+
5+
// 解码方法
6+
// 动态规划,dp[i]表示数字串前i个数字所有的解码方案
7+
public class Main045 {
8+
public static void main(String[] args) {
9+
Scanner sc = new Scanner(System.in);
10+
String str = sc.next();
11+
12+
int n = str.length();
13+
int[] dp = new int[n + 1];
14+
dp[0] = 1;
15+
for (int i = 1; i <= n; i++) {
16+
// 当前字符未组合时,当前解码方案等于上一种解码方案
17+
if (str.charAt(i - 1) != '0')
18+
dp[i] += dp[i - 1];
19+
// 当前字符可组合时,解码方案加上'去除最后两个字符时的解码方案'
20+
if (i >= 2 && (str.charAt(i - 2) == '1' || (str.charAt(i - 2) == '2' && str.charAt(i - 1) <= '6')))
21+
dp[i] += dp[i - 2];
22+
}
23+
System.out.println(dp[n]);
24+
}
25+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package CampusRecruitment2019;
2+
3+
import java.util.Scanner;
4+
import java.util.Arrays;
5+
6+
// 漂流船问题
7+
// 贪心,先排序数组,再用双指针从左右两端向内移动,两人体重和满足限制条件则两个人一只船,否则重的人单独一只船
8+
public class Main046 {
9+
public static void main(String[] args) {
10+
Scanner sc = new Scanner(System.in);
11+
String[] people = sc.nextLine().split(" ");
12+
int limit = sc.nextInt();
13+
14+
Arrays.sort(people);
15+
int l = 0, r = people.length - 1, res = 0;
16+
while (l <= r) {
17+
if (Integer.parseInt(people[l]) + Integer.parseInt(people[r]) <= limit) {
18+
l++;
19+
r--;
20+
} else {
21+
r--;
22+
}
23+
res += 1;
24+
}
25+
System.out.println(res);
26+
}
27+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package CampusRecruitment2019;
2+
3+
import java.util.Scanner;
4+
5+
// 排队唱歌
6+
// 直接插入排序并记录交换次数
7+
public class Main049 {
8+
public static void main(String[] args) {
9+
Scanner sc = new Scanner(System.in);
10+
int n = sc.nextInt();
11+
int[] high = new int[n];
12+
for (int i = 0; i < n; i++)
13+
high[i] = sc.nextInt();
14+
15+
int res = 0;
16+
for (int i = 1; i < n; i++) {
17+
for (int j = i - 1; j >= 0; j--) {
18+
if (high[j] > high[j + 1]) {
19+
int temp = high[j];
20+
high[j] = high[j + 1];
21+
high[j + 1] = temp;
22+
res += 1;
23+
} else {
24+
break;
25+
}
26+
}
27+
}
28+
System.out.println(res);
29+
}
30+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package CampusRecruitment2019;
2+
3+
import java.util.Comparator;
4+
import java.util.Scanner;
5+
import java.util.Arrays;
6+
7+
// 挑选代表
8+
public class Main051 {
9+
public static void main(String[] args) {
10+
Scanner sc = new Scanner(System.in);
11+
int n = sc.nextInt();
12+
int[][] region = new int[n][2];
13+
for (int i = 0; i < n; i++) {
14+
region[i][0] = sc.nextInt();
15+
region[i][1] = sc.nextInt();
16+
}
17+
18+
Arrays.sort(region, new Comparator<int[]>() {
19+
@Override
20+
public int compare(int[] a, int[] b) {
21+
// 差大于0则交换位置,按照数组第二个元素升序排序
22+
return a[1] - b[1];
23+
}
24+
});
25+
// 取区间的最后两个数
26+
int p2 = region[0][1] - 1, p1 = region[0][1], res = 2;
27+
for (int i = 1; i < n; i++) {
28+
int a = region[i][0];
29+
int b = region[i][1];
30+
// 当前区间包含了前一区间的两个数
31+
if (a <= p1 && p1 <= b && a <= p2 && p2 <= b)
32+
continue;
33+
// 当前区间包含了前一区间的一个数
34+
else if (a <= p1 && p1 <= b) {
35+
p2 = p1;
36+
p1 = b;
37+
res += 1;
38+
// 当前区间不包含前一区间的数,需要获取当前区间的最后两个数
39+
} else {
40+
p2 = b - 1;
41+
p1 = b;
42+
res += 2;
43+
}
44+
}
45+
System.out.println(res);
46+
}
47+
}

0 commit comments

Comments
 (0)