逻辑题
逻辑题
OD253. 流水线
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] split = in.nextLine().split(" ");
int m = Integer.parseInt(split[0]); // 流水线
int n = Integer.parseInt(split[1]); // 作业数
String[] temp = in.nextLine().split(" ");
int[] arr = new int[temp.length];
for (int i = 0; i < arr.length; i++) {
arr[i] = Integer.parseInt(temp[i]);
}
Arrays.sort(arr);
int res = 0;
int[] sum = new int[m];
for (int i = 0; i < arr.length; i++) {
sum[i % m] += arr[i];
}
for (int i : sum) {
res = Math.max(res, i);
}
System.out.println(res);
}
}
OD178. 单词接龙
建立一个 map<Character, List
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int startIndex = Integer.parseInt(in.nextLine());
int N = Integer.parseInt(in.nextLine());
String start = "";
Map<Character, List<String>> map = new HashMap<>();
for (int i = 0; i < N; i++) {
String s = in.nextLine();
if (i == startIndex){
start = s;
continue;
}
if (map.containsKey(s.charAt(0))){
List<String> temp = map.get(s.charAt(0));
temp.add(s);
map.put(s.charAt(0), temp);
}else {
ArrayList<String> temp = new ArrayList<>();
temp.add(s);
map.put(s.charAt(0), temp);
}
}
for (Character character : map.keySet()) {
List<String> temp = map.get(character);
Collections.sort(temp, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
int length1 = o1.length();
int length2 = o2.length();
if (length1 != length2){
return length2 - length1;
}
return o1.compareTo(o2);
}
});
map.put(character, temp);
}
StringBuilder res = new StringBuilder();
res.append(start);
char end = start.charAt(start.length() - 1);
while (map.containsKey(end) && map.get(end).size() > 0){
List<String> list1 = map.get(end);
String s = list1.get(0);
res.append(s);
list1.subList(0, 1).clear();
map.put(end, list1);
end = s.charAt(s.length() -1); // 更新 end
}
System.out.println(res);
}
}
304. 5键键盘
3种情况会清空选择的串:
- a:输出
- 剪切
- 复制粘贴
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] split = in.nextLine().split(" ");
int[] arr = new int[split.length];
for (int i = 0; i < arr.length; i++) {
arr[i] = Integer.parseInt(split[i]);
}
StringBuilder screen = new StringBuilder();
StringBuilder board = new StringBuilder();
String selectStr = "";
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 1){ // a
if (!selectStr.equals("")){
screen.delete(screen.length() - selectStr.length(), selectStr.length());
screen.append("a");
selectStr = "";
continue;
}
screen.append("a");
}else if (arr[i] == 2){ // copy ==> board
if (selectStr.equals("")){
continue;
}
board = new StringBuilder();
board.append(selectStr);
}else if (arr[i] == 3){ // x:剪切到 board
if (selectStr.equals("")){
continue;
}
screen.delete(screen.length() - selectStr.length(), selectStr.length());
board = new StringBuilder();
board.append(selectStr);
selectStr = "";
}else if (arr[i] == 4){ // 复制粘贴板到 screen
if (selectStr.equals("")){
screen.append(board);
continue;
}
screen.delete(screen.length() - selectStr.length(), selectStr.length());
screen.append(board);
selectStr = "";
}else if (arr[i] == 5){ // 选择屏幕所有字母
if (screen.length() == 0){
continue;
}
selectStr = screen.toString();
}
}
System.out.println(screen.length());
}
}
210. 停车场车辆统计
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] split = in.nextLine().split(",");
int[] arr = new int[split.length];
for (int i = 0; i < arr.length; i++) {
arr[i] = Integer.parseInt(split[i]);
}
int res = 0;
for (int i = 0; i < arr.length; i++) {
int index = i;
if (arr[i] == 1){
res++; // 1. 当前
while (i + 1 < arr.length && i < index + 2 && arr[i + 1] == 1){ // 2. i < index:看能够再向右推进2位
i++;
}
}
}
System.out.println(res);
}
}
314. 解密犯罪时间
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.nextLine();
String[] split = s.split(":");
int hour = Integer.parseInt(split[0]);
int minute = Integer.parseInt(split[1]);
String[][] arr = new String[24][60];
String target = s.replace(":", "");
for (int i = 0; i < 24; i++) {
for (int j = 0; j < 60; j++) {
String h = i+"";
String m = j+"";
if (i < 10){
h = "0" + h;
}
if (j < 10){
m = "0" + m;
}
arr[i][j] = h + m;
}
}
for (int i = hour; i < 24; i++) { // 后方
for (int j = minute + 1; j < 60; j++) {
if (isMatch(arr[i][j], target)){
getResult(arr[i][j]);
return;
}
}
minute = -1;
}
for (int i = 0; i < 24; i++) { // 前方
for (int j = 0; j < 60; j++) {
if (isMatch(arr[i][j], target)){
getResult(arr[i][j]);
return;
}
}
}
}
public static boolean isMatch(String str, String target){
for (char c : str.toCharArray()) {
if (!target.contains(c+"")){
return false;
}
}
return true;
}
public static void getResult(String str){
StringBuilder sb = new StringBuilder(str);
sb.insert(2, ":");
System.out.println(sb);
}
}
126.VLAN资源池
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
static String[] split;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// VLAN ID(1-4094之间的整数)
String ss = in.nextLine();
if (ss.contains(",")) {
split = ss.split(",");
} else {
split = new String[]{ss};
}
int target = Integer.parseInt(in.nextLine());
List<Num> list = new ArrayList<>();
List<Num> res = new ArrayList<>();
for (String s : split) {
if (s.contains("-")) {
String[] temp = s.split("-");
int left = Integer.parseInt(temp[0]);
int right = Integer.parseInt(temp[1]);
list.add(new Num(left, right));
} else {
int left = Integer.parseInt(s);
list.add(new Num(left, left));
}
}
for (Num num : list) {
if (num.left == num.right) { // 只有一个
if (target != num.left) {
res.add(num);
}
} else { // 多个
if (target == num.left) {
res.add(new Num(num.left + 1, num.right));
} else if (target == num.right) {
res.add(new Num(num.left, num.right - 1));
} else if (target < num.right && target > num.left) {
res.add(new Num(num.left, target - 1));
res.add(new Num(target + 1, num.right));
}else {
res.add(num);
}
}
}
StringBuilder sb = new StringBuilder();
res.stream().sorted((o1, o2) -> o1.left - o2.left).forEach(num -> {
if (num.left == num.right) {
sb.append(num.left + ",");
} else {
sb.append(num.left + "-" + num.right + ",");
}
});
System.out.println(sb.toString().substring(0, sb.length() - 1));
}
}
class Num {
int left;
int right;
public Num(int left, int right) {
this.left = left;
this.right = right;
}
}
144. 稀疏矩阵扫描
1. 连续版本
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
static int row;
static int col;
static int res1;
static int res2;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
row = in.nextInt();
col = in.nextInt();
int[][] arr = new int[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
arr[i][j] = in.nextInt();
}
}
for (int i = 0; i < row; i++) {
if (getCount(arr[i]) >= col / 2){
res1++;
}
}
System.out.println(res1);
int[] arr2 = new int[row];
for (int i = 0; i < col; i++) { // 行数
int index = 0;
for (int j = 0; j < row; j++) {
arr2[index++] = arr[j][i];
}
if (getCount(arr2) >= row / 2){
res2++;
}
}
System.out.println(res2);
}
public static int getCount(int[] arr){ // 每一行
int res = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 0){
int count = 1;
while (i + 1 < arr.length && arr[i + 1] == 0){
count++;
i++;
}
res = Math.max(res, count);
}
}
return res;
}
}
2. 不连续版本
极端用例:
18 1
0
1
0
0
0
0
1
1
1
1
1
0
1
1
0
1
1
0
# 输出 18 0
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
static int row;
static int col;
static int res1;
static int res2;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
row = in.nextInt();
col = in.nextInt();
int[][] arr = new int[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
arr[i][j] = in.nextInt();
}
}
for (int i = 0; i < row; i++) {
if (getCount(arr[i]) >= col / 2){
res1++;
}
}
System.out.println(res1);
int[] arr2 = new int[row];
for (int i = 0; i < col; i++) { // 行数
int index = 0;
for (int j = 0; j < row; j++) {
arr2[index++] = arr[j][i];
}
if (getCount(arr2) >= row / 2){
res2++;
}
}
System.out.println(res2);
}
public static int getCount(int[] arr){ // 每一行
int res = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 0){
res++;
}
}
return res;
}
}
289. 整数对最小和
双重 for 循环
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
static int[] arr1;
static int[] arr2;
static int count;
static int sum = 0;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] split1 = in.nextLine().trim().split(" ");
String[] split2 = in.nextLine().trim().split(" ");
arr1 = new int[Integer.parseInt(split1[0])];
arr2 = new int[Integer.parseInt(split2[0])];
for (int i = 0; i < arr1.length; i++) {
arr1[i] = Integer.parseInt(split1[i + 1]);
}
for (int i = 0; i < arr2.length; i++) {
arr2[i] = Integer.parseInt(split2[i + 1]);
}
count = in.nextInt();
List<Integer> list = new ArrayList<>();
for (int i : arr1) {
for (int j : arr2) {
list.add(i + j);
}
}
list.stream().sorted(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1 - o2;
}
}).limit(count).forEach(integer -> sum += integer);
System.out.println(sum);
}
}
OD389. 全排列【求解无重复的全排列个数】
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.nextLine();
Map<Character, Integer> map = new HashMap<>();
for (char c : s.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
long sum = getSum(s.length());
for (Integer value : map.values()) {
if (value > 1){
sum /= getSum(value); // 除去次数超过1 的个数的阶乘!!!
}
}
System.out.println(sum);
}
public static long getSum(long n){
if (n == 1){
return 1;
}
return n * getSum(n - 1);
}
}
219. 执行时长
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int time = in.nextInt();
int count = in.nextInt();
int res = 0;
int sum = 0; // 类即值
int[] arr = new int[count];
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}
for (int i : arr) {
int now = i + sum; // 当前 + 现在
sum = Math.max(0, now - time);
res++;
}
System.out.println((int)(res + Math.ceil(sum / (double)time)));
}
}
305. 分班
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
static List<Integer> list1 = new ArrayList<>();
static List<Integer> list2 = new ArrayList<>();
static boolean flag = false;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.nextLine();
String[] split = s.split(" ");
for (int i = 0; i < split.length; i++) {
String[] temp = split[i].split("/");
int No = Integer.parseInt(temp[0]);
if (No < 0 || No > 999){
System.out.println("ERROR");
return;
}
String isSame = temp[1];
if (i == 0 && "Y".equals(isSame)){
System.out.println("ERROR");
return;
}
if ("N".equals(isSame)){
flag = !flag;
}
add(No);
}
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
if (list1.size() == 0){
Collections.sort(list2);
for (Integer integer : list2) {
sb1.append(integer + " ");
}
System.out.println(sb1.toString().trim());
return;
}
if (list2.size() == 0){
Collections.sort(list1);
for (Integer integer : list1) {
sb2.append(integer + " ");
}
System.out.println(sb2.toString().trim());
return;
}
Collections.sort(list1);
Collections.sort(list2);
for (Integer integer : list1) {
sb1.append(integer + " ");
}
for (Integer integer : list2) {
sb2.append(integer + " ");
}
if (list1.get(0) < list2.get(0)){
System.out.println(sb1.toString().trim());
System.out.println(sb2.toString().trim());
return;
}
System.out.println(sb2.toString().trim());
System.out.println(sb1.toString().trim());
}
public static void add(int No){
if (!flag){ // 第一个默认为 false;
list1.add(No);
}else {
list2.add(No);
}
}
}
135. 告警抑制
特殊用例:
# 读取到 A10,应为 A0。
# 使用 List 来规避
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
static Map<String, List<String>> map = new HashMap<>(); // 【被抑制的,抑制者】:如果当前有已知者,不输出当前
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int count = Integer.parseInt(in.nextLine());
for (int i = 0; i < count; i++) {
String[] split = in.nextLine().split(" ");
String attack = split[0];
String now = split[1];
List<String> orDefault = map.getOrDefault(now, new ArrayList<>());
orDefault.add(attack);
map.put(now, orDefault);
}
String target = in.nextLine();
String[] split = target.split(" ");
List<String> targetList = new ArrayList<>();
for (String s : split) {
targetList.add(s);
}
StringBuilder res = new StringBuilder();
for (String s : split) {
if (isValid(s, targetList)){
res.append(s + " ");
}
}
System.out.println(res.toString().trim());
}
public static boolean isValid(String str, List targetList){
if (!map.containsKey(str)){ // 不是被抑制者
return true;
}
for (String s : map.get(str)) { // 如果当前有当前的抑制者
if (!str.equals(s) && targetList.contains(s)){ // 不能循环抑制【即:不能抑制自己】 !!!
return false;
}
}
return true;
}
}
170. 比赛的冠亚季军
注意点:
- 输入值可能会大于 Integer最大值,所以要用 Long.parseLong() 来解析
- 最后考虑剩下 3个 或者 4个的情形:使用2个集合,big和small
- 4个:big和small各有2个,取出big中的2个和small中较大的
- 3个:big有2个,small有一个
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
static List<Gamer> list = new ArrayList<>();
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] split = in.nextLine().split(" ");
for (int i = 0; i < split.length; i++) {
list.add(new Gamer(i, Long.parseLong(split[i])));
}
play();
ArrayList<Gamer> big = new ArrayList<>();
ArrayList<Gamer> small = new ArrayList<>();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.size(); i++) {
Gamer now = list.get(i);
if (i + 1 < list.size()){
if (now.compareTo(list.get(i + 1)) > 0){
big.add(now);
small.add(list.get(i + 1));
}else {
small.add(now);
big.add(list.get(i + 1));
}
i++;
}else {
big.add(now); // 1. big 至少有 2个
}
}
if (big.get(0).compareTo(big.get(1)) > 0){ // 2. 在 big 中决出 冠亚军
sb.append(big.get(0).id + " ");
sb.append(big.get(1).id + " ");
}else {
sb.append(big.get(1).id + " ");
sb.append(big.get(0).id + " ");
}
if (small.size() == 1){ // 3. 在small 中决出 季军
sb.append(small.get(0).id + "");
}else {
if (small.get(0).compareTo(small.get(1)) > 0){
sb.append(small.get(0).id);
}else {
sb.append(small.get(1).id);
}
}
System.out.println(sb);
}
public static void play() {
if (list.size() == 3 || list.size() == 4) {
return;
}
ArrayList<Integer> temp = new ArrayList<>(); // 记录要删的
for (int i = 0; i < list.size(); i++) {
Gamer gamer = list.get(i);
if (i + 1 < list.size()) {
if (gamer.compareTo(list.get(i + 1)) < 0) {
temp.add(i); // 标记小的
} else {
temp.add(i + 1); // 标记小的
}
i++;
}
}
for (int j = temp.size() - 1; j >= 0; j--) {
Integer delIndex = temp.get(j);
list.subList(delIndex, delIndex + 1).clear(); // 从右侧开始删除
}
play();
}
}
class Gamer implements Comparable<Gamer> {
int id;
long ability;
public Gamer(int id, long ability) {
this.id = id;
this.ability = ability;
}
@Override
public int compareTo(Gamer o) {
// 每一轮,相邻的运动员进行比赛,获胜的进入下一轮
// 1. 实力值大的获胜
// 2. 实力值相等的情况,id小的情况下获胜
if (this.ability != o.ability) {
return Long.compare(this.ability, o.ability);
}
return Long.compare(o.id, this.id);
}
}
184. 判断一组不等式是否满足约束并输出最大差
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
static int row;
static int col;
static double[][] arr ;
static double[] x;
static double[] b;
static String[] symbols; // 符号
static long res = Long.MIN_VALUE;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.nextLine();
int index = s.lastIndexOf(";");
String leftString = s.substring(0, index); // a 和 x
String[] split = leftString.split(";"); // 1. 0 - row:a row:x row + 1:b
col = split[0].split(",").length;
String rightString = s.substring(index + 1); // res
row = rightString.split(",").length;
symbols = new String[row];
String[] split3 = rightString.split(",");
for (int i = 0; i < symbols.length; i++) {
symbols[i] = split3[i];
}
arr = new double[row][col];
x = new double[col];
b = new double[row];
for (int i = 0; i < row; i++) {
String temp = split[i];
for (int j = 0; j < col; j++) {
arr[i][j] = Double.parseDouble(temp.split(",")[j]);
}
}
String[] split1 = split[row].split(",");
for (int i = 0; i < x.length; i++) {
x[i] = Double.parseDouble(split1[i]);
}
String[] split2 = split[row + 1].split(",");
for (int i = 0; i < split2.length; i++) {
b[i] = Double.parseDouble(split2[i]);
}
boolean flag = true;
for (int i = 0; i < arr.length; i++) {
double[] a = arr[i];
double sum = getSum(a, x); // 左侧的和
if (!isTrue(sum, b[i], symbols[i])){
flag = false;
}
res = Math.max(res, (long)(sum - b[i]));
}
System.out.print(flag + " ");
System.out.print(res);
}
public static double getSum(double[] arr1, double[] x){
double sum = 0;
for (int i = 0; i < arr1.length; i++) {
sum += arr1[i] * x[i];
}
return sum;
}
public static boolean isTrue(double left, double right, String symbol){
if (">".equals(symbol)){
return left > right;
}
if ("<".equals(symbol)){
return left < right;
}
if (">=".equals(symbol)){
return left >= right;
}
if ("<=".equals(symbol)){
return left <= right;
}
return left == right;
}
}
176. 阿里巴巴找黄金宝箱(Ⅲ)
歧义点:
用例:
1,2,2,1
10
# 如果以先找到这对宝箱的左边的宝箱为“最先”,则这对宝箱贴的数字应该是1
# 如果以先找到这对宝箱的右边的宝箱为“最先”,则这对宝箱贴的数字应该是2【⭐】
以右侧为基准(只要找到了一对,就输出左侧)
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] split = in.nextLine().split(",");
int magic = in.nextInt();
int[] arr = new int[split.length];
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < arr.length; i++) {
arr[i] = Integer.parseInt(split[i]);
if (!map.containsKey(arr[i])){
map.put(arr[i], i);
continue;
}
Integer integer = map.get(arr[i]);
if (i - integer <= magic){
System.out.println(integer);
return;
}else {
map.put(arr[i], i);
}
}
System.out.println(-1);
}
}
以左侧为基准(输出左侧的最小值)
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] split = in.nextLine().split(",");
int magic = in.nextInt();
int[] arr = new int[split.length];
Map<Integer, Integer> map = new HashMap<>();
int res = Integer.MAX_VALUE;
for (int i = 0; i < arr.length; i++) {
arr[i] = Integer.parseInt(split[i]);
if (!map.containsKey(arr[i])){
map.put(arr[i], i);
continue;
}
Integer integer = map.get(arr[i]);
if (i - integer <= magic){
res = Math.min(res, integer); // 保存左侧最小值
}else {
map.put(arr[i], i);
}
}
if (res == Integer.MAX_VALUE){
System.out.println(-1);
return;
}
System.out.println(res);
}
}
132. AI面板识别【list.removeAll()方法应用、先按照 y1排序、再按照 x1排序。球的半径:(y2 - y1) / 2】
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int count = in.nextInt();
List<Ball> list = new ArrayList<>();
for (int i = 0; i < count; i++) {
list.add(new Ball(in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt()));
}
Collections.sort(list, new Comparator<Ball>() {
@Override
public int compare(Ball o1, Ball o2) {
if (o1.y1 != o2.y1) {
return o1.y1 - o2.y1;
}
return o1.x1 - o2.x1;
}
});
StringBuilder res = new StringBuilder();
while (list.size() != 0) {
Ball now = list.get(0);
res.append(now.id + " ");
List<Ball> temp = new ArrayList<>();
for (int j = 1; j < list.size(); j++) {
Ball ball = list.get(j);
if (Math.abs(now.y1 - ball.y1) <= ball.getRadius()) { // 同行的!!!
temp.add(ball);
}
}
if (!temp.isEmpty()) {
temp.stream().sorted((o1, o2) -> o1.x1 - o2.x1).forEach(ball -> res.append(ball.id + " "));
list.removeAll(temp);
}
list.remove(0);
}
System.out.println(res.toString().trim());
}
}
class Ball{
int id;
int x1;
int y1;
int x2;
int y2;
public Ball(int id, int x1, int y1, int x2, int y2) {
this.id = id;
this.x1 = x1;
this.y1 = y1; // 越小越高!!!
this.x2 = x2;
this.y2 = y2;
}
public int getRadius(){
return (y2 - y1) / 2;
}
}
292. ABR车路协同场景
import java.util.Scanner;
import java.util.*;
import java.util.regex.*;;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.nextLine();
int R = Integer.parseInt(s.substring(s.indexOf("R") + 2));
String reg = "\\{[0-9,]+}";
Matcher matcher = Pattern.compile(reg).matcher(s);
List<List<Integer>> list = new ArrayList<>();
while (matcher.find()){
String group = matcher.group();
ArrayList<Integer> temp = new ArrayList<>();
String[] split = group.substring(1, group.length() - 1).split(",");
for (String s1 : split) {
temp.add(Integer.parseInt(s1));
}
list.add(temp);
}
List<Integer> list1 = list.get(0);
List<Integer> list2 = list.get(1);
StringBuilder res = new StringBuilder();
for (int i = 0; i < list1.size(); i++) {
Integer A = list1.get(i);
boolean flag = false;
for (int j = 0; j < list2.size(); j++) {
Integer B = list2.get(j);
if (A <= B && A + R >= B){
flag = true;
res.append("(" + A + "," + B + ")");
}
}
if (!flag){ // 没找到
for (int j = 0; j < list2.size(); j++) {
if (list2.get(j) >= A){
res.append("(" + A + "," + list2.get(j) + ")");
break;
}
}
}
}
System.out.println(res);
}
}
175. 响应报文时间【Integer.parseInt("str", 2):按照二进制来转换,默认是十进制】
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int count = in.nextInt();
List<Message> list = new ArrayList<>();
for (int i = 0; i < count; i++) {
list.add(new Message(in.nextInt(), in.nextInt()));
}
int res = Integer.MAX_VALUE;
for (Message message : list) {
res = Math.min(res, message.req + getMaxRespCode(message.resp));
}
System.out.println(res);
}
public static int getMaxRespCode(int time){
// 最大响应时间有如下计算方式:
// 1. Max Resp Code < 128, Max Resp Time = Max Resp Code
// 2. Max Resp Code ≥ 128
// Max Resp Time = (mant | 0x10) << (exp + 3);
// - exp最大响应时间的高5~7位 0b01110000
// - mant 为最大响应时间的低4位:0b00001111
// 认为 HOST收到查询报文时,选取的随机时间必定为最大值【即:都是右侧字段回复!!】
if (time < 128){
return time;
}
StringBuilder sb = new StringBuilder(Integer.toBinaryString(time));
while (sb.length() < 8){
sb.insert(0, "0");
}
int exp = Integer.parseInt(sb.substring(1, 4), 2);
int mant = Integer.parseInt(sb.substring(4), 2);
return (mant|0x10)<<(exp + 3);
}
}
class Message{
int req;
int resp;
public Message(int req, int resp) {
this.req = req;
this.resp = resp;
}
}
129. 整数编码【每7位一组,sb.insert(0, "str") 应用】
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long num = in.nextLong();
String string = Long.toBinaryString(num);
while (string.length() % 7 != 0){
string = "0" + string;
}
String coding = string;
StringBuilder res = new StringBuilder();
for (int i = 0; i < coding.length() / 7; i++) { // 每7个作为一组去处理!!!
String substring= "";
if (i == 0){
substring = "0" + coding.substring(0, 7);
}else {
substring = "1" + coding.substring(i * 7, i * 7 + 7);
}
res.insert(0, coding(substring));
}
System.out.println(res.toString().toUpperCase());
}
public static String coding(String s){
long l = Long.parseLong(s, 2);
String string = Long.toHexString(l);
if (string.length() < 2){
return "0" + string;
}
return string;
}
}
250. TVL解析Ⅰ【Integer.parseInt(str, 16),小端序】
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String key = in.nextLine();
String[] split = in.nextLine().split(" ");
StringBuilder res = new StringBuilder();
for (int i = 0; i < split.length; i++) {
String s = split[i];
if (key.equals(s)){
StringBuilder sb = new StringBuilder();
StringBuilder temp = sb.append(split[i + 2]).append(split[i + 1]);
int count = Integer.parseInt(temp.toString(), 16);
for (int j = 0; j < count; j++) {
res.append(split[i + 3 + j] + " ");
}
break;
}else {
StringBuilder sb = new StringBuilder();
StringBuilder temp = sb.append(split[i + 2]).append(split[i + 1]);
int count = Integer.parseInt(temp.toString(), 16);
i = i + 2 + count;
}
}
System.out.println(res.toString().trim().toUpperCase());
}
}
320.绘图机器
思路:
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] split = in.nextLine().split(" ");
int count = Integer.parseInt(split[0]);
int xEnd = Integer.parseInt(split[1]);
long res = 0;
long height = 0;
ArrayList<Integer[]> list = new ArrayList<>();
for (int i = 0; i < count; i++) {
int x = in.nextInt();
int offsetY = in.nextInt();
list.add(new Integer[]{x, offsetY});
}
list.add(new Integer[]{xEnd, xEnd}); // 为了取得最后一部分的宽度
for (int i = 0; i < list.size(); i++) {
Integer[] integers = list.get(i);
Integer x0 = integers[0];
Integer offset = integers[1];
height += offset;
if (i + 1 < list.size()){
Integer[] temp = list.get(i + 1);
Integer x1 = temp[0];
res += (x1 - x0) * Math.abs(height);
}
}
System.out.println(res);
}
}
255. 用户调用问题
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int res = 0;
int count = Integer.parseInt(in.nextLine());
boolean[] flags = new boolean[3];
List<job> list = new ArrayList<>();
for (int i = 0; i < count; i++) { // 共有多少行
list.add(new job(0, in.nextInt()));
list.add(new job(1, in.nextInt()));
list.add(new job(2, in.nextInt()));
// 多个满足要求的策略,选最后一个 ===> id 倒序
Collections.sort(list, (o1, o2) -> {
if (o1.value != o2.value){
return o1.value - o2.value;
}
return o2.index - o1.index;
});
for (int j = 0; j < list.size(); j++) {
job job = list.get(j);
if (!flags[job.index]){ // 最小的还未选过的策略
res += job.value;
Arrays.fill(flags, false);
flags[job.index] = true; // 更新最新被选用的策略
break;
}
}
list.clear();
}
System.out.println(res);
}
}
class job{
int index;
int value;
public job(int index, int value) {
this.index = index;
this.value = value;
}
}
141. 模拟消息队列
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] split = in.nextLine().split(" ");
List<Mes> list1 = new ArrayList<>();
List<Consumer> list2 = new ArrayList<>();
for (int i = 0; i < split.length; i += 2) {
int start = Integer.parseInt(split[i]);
int info = Integer.parseInt(split[i + 1]);
list1.add(new Mes(start, info));
}
String[] split1 = in.nextLine().split(" ");
int priority = 0;
for (int i = 0; i < split1.length; i+= 2) {
int start = Integer.parseInt(split1[i]);
int end = Integer.parseInt(split1[i + 1]);
list2.add(new Consumer(priority++, start, end)); // 优先级、起始、末尾
}
Collections.sort(list1, (o1, o2) -> o1.start - o2.start);
Map<Integer, String> res = new TreeMap<>();
for (int i = 0; i < list1.size(); i++) {
Mes mes = list1.get(i);
for (int j = list2.size() - 1; j >= 0; j--) {
Consumer consumer = list2.get(j);
// 1. 能订阅上
// 2. 门已经关闭
if (mes.start >= consumer.start && consumer.end > mes.start){
res.put(consumer.priority, res.getOrDefault(consumer.priority, "") + mes.info + " ");
break;
}
}
}
for (Consumer consumer : list2) {
System.out.println(res.getOrDefault(consumer.priority, "-1"));
}
}
}
class Mes{
int start;
int info;
public Mes(int start, int info) {
this.start = start;
this.info = info;
}
}
class Consumer{
int priority;
int start;
int end;
public Consumer(int priority, int start, int end) {
this.priority = priority;
this.start = start;
this.end = end;
}
}
264. 不开心的小朋友
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int count = Integer.parseInt(in.nextLine());
String[] split = in.nextLine().split(" ");
int[] arr = new int[split.length];
for (int i = 0; i < arr.length; i++) {
arr[i] = Integer.parseInt(split[i]);
}
List<Integer> list = new ArrayList<>();
Deque<Integer> deque = new ArrayDeque<>(); // 等待队列
int res = 0;
// 没有空余的摇摇车,需要排队等候,或者直接离开,最后没有玩上的小朋友会非常不开心。
for (int i : arr) {
if (list.size() == 0){
list.add(i);
continue;
}
//
if (list.size() < count){ // 有空位说明没有等待的
if (list.contains(i)){
list.remove(Integer.valueOf(i));
}else {
list.add(i);
}
}else if (list.size() == count){ // 满了
if (list.contains(i)){
list.remove(Integer.valueOf(i));
if (!deque.isEmpty()){
list.add(deque.pop());
}
}else { // 赖着不走,倒不出空位
if (!deque.isEmpty()){
if (deque.contains(i)){
deque.remove(i);
res++;
}else {
deque.offer(i);
}
}else {
deque.offer(i);
}
}
}
}
while (!deque.isEmpty()){
res++;
deque.poll();
}
System.out.println(res);
}
}
191. 求最小步数
// 本题数量级很大,可以利用 dp 来辅助寻找规律
public class smallest {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int target = in.nextInt();
int[] dp = new int[target + 1];
dp[0] = 0;
dp[1] = 2;
dp[2] = 1;
dp[3] = 1;
for (int i = 4; i < dp.length; i++) {
dp[i] = Math.min(dp[i - 2], dp[i - 3]) + 1;
}
System.out.println(dp[target]);
}
}
步数 | 除 3 后的余数 | |
---|---|---|
1 | 2 | |
2 | 1 | |
3 | 1 | |
4 | 2 | 1 |
5 | 2 | 2 |
6 | 2 | 0 |
7 | 3 | 1 |
8 | 3 | 2 |
9 | 3 | 0 |
10 | 4 | 1 |
11 | 4 | 2 |
12 | 4 | 0 |
13 | 5 | 1 |
14 | 5 | 2 |
15 | 5 | 0 |
16 | 6 | 1 |
17 | 6 | 2 |
得到规律如下: |
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int target = in.nextInt();
if (target == 1){
System.out.println(2);
return;
}
if (target % 3 == 0){
System.out.println(target / 3);
return;
}
System.out.println((target + (3 - target % 3)) / 3);
}
}
171. 经典屏保
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x = in.nextInt();
int y = in.nextInt();
int t = in.nextInt();
x = x + t;
y = y + t;
while (x + 50 > 800 || y + 25 > 600 || x < 0 || y < 0){
if (x + 50 > 800){
x = (800 - 50) - (x - (800 - 50));
}
if (y + 25 > 600){
y = (600 - 25) - (y - (600 - 25));
}
if (x < 0){
x = -x;
}
if (y < 0){
y = -y;
}
}
System.out.print(x + " " + y);
}
}