临近蓝桥杯,多多磨练自己心态,多打打校赛~
一、素数送温暖
直接从x+1开始算素数即可
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static boolean check(int x) {
for (int i = 2; i <= (int)(Math.sqrt(x)); i++) {
if (x % i == 0) return false;
}
return true;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int x = scan.nextInt();
for (int i = x + 1; ;i++) {
if (check(i)) {
System.out.println(i);
break;
}
}
}
}
二、睡懒觉
直接模拟就行,注意分钟60,小时+1,注意小时=24时,置为0。
import java.util.*;
import java.io.*;
public class Main {
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException {
// 按照:分成两坨
String[] input = reader.readLine().trim().split(":");
int a = Integer.parseInt(input[0]);
int b = Integer.parseInt(input[1]);
int ans = 0;
while (check(a, b) == false) {
// a是小时,b是分钟
b++;
ans++;
if (b == 60) {
a++;
b = 0;
}
if (a == 24) a = 0;
}
writer.write(ans + "");
writer.flush();
}
// 判断是否为回文
static public boolean check(int a, int b) {
if(a / 10 == b % 10 && a % 10 == b/10) return true;
else return false;
}
}
三、数字找朋友
这道题,我看数据以为有啥坑,先写个爆搜,直接就过了??嗯???
import java.util.*;
import java.io.*;
public class Main {
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
static boolean[] vis;
public static void main(String[] args) throws IOException {
String[] input = reader.readLine().trim().split(" ");
int a = Integer.parseInt(input[0]);
int b = Integer.parseInt(input[1]);
if (dfs(a, b, 0)) {
writer.write("YES");
} else {
writer.write("NO");
}
writer.flush();
}
static boolean dfs(long a, long b, int step) {
if (step > 10) return false;
if (a == b) return true;
return dfs(a * 3, b, step + 1) || dfs(a * 5, b, step + 1) || dfs(a / 7, b, step + 1);
}
}
四、爬虫数据解析
java被卡时间了,最后一个点过不去。
import java.util.*;
import java.io.*;
public class Main {
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
static boolean[] vis;
public static void main(String[] args) throws IOException {
String[] input = reader.readLine().trim().split(",");
int n = input.length;
// 记录key-value对
HashMap<String, List<String>> map = new HashMap<>();
// 记录所有key,方便
String[] key = new String[200001];
int index = 0;
for (int i = 0; i < n; i++) {
String tmp = "";
if (i == 0) {
tmp = input[i].substring(1, input[i].length());
} else if (i + 1 == n) {
tmp = input[i].substring(0, input[i].length() - 1);
} else {
tmp = input[i];
}
// 用 : 切分开
String[] json = tmp.split(":");
if (!map.containsKey(json[0])) {
key[index++] = json[0];
// 没有当前key
List<String> list = new ArrayList<>();
list.add(json[1]);
map.put(json[0], list);
} else {
List<String> list = map.get(json[0]);
list.add(json[1]);
map.put(json[0], list);
}
}
Arrays.sort(key, 0, index);
for (int i = 0; i < index; i++) {
List<String> list = map.get(key[i]);
Collections.sort(list);
for (String cur : list) {
writer.write(key[i] + " " + cur + "\n");
}
}
writer.flush();
}
}
五、加密算法
纯纯模拟题,java代码直接超题目内存限制(属于是根本没对java进行宽松处理…只能用c/c++写)
import java.util.*;
import java.io.*;
public class Main {
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException {
String ming = reader.readLine().trim();
String secret = reader.readLine().trim();
// 全部转成大写
ming = ming.toUpperCase(Locale.ROOT);
secret = secret.toUpperCase(Locale.ROOT);
boolean[] vis = new boolean[26];
int len = secret.length();
char[][] matrix = new char[5][5]; // 构造 5x5 密钥矩阵
int a = 0, b = 0; // 密钥矩阵下标
for (int i = 0; i < len; i++) {
if (b == 5) {
a++;
b = 0;
if (a == 5) break;
}
char tmp = secret.charAt(i);
if (vis[tmp - 'A']) { // 字母已经被选过了
continue;
} else { // 字母还没被选过
if (tmp == 'J') { // j调换为i
vis['J' - 'A'] = true;
if (vis['I' - 'A']) {
continue;
} else {
vis['I' - 'A'] = true;
matrix[a][b++] = 'I';
}
} else {
vis[tmp - 'A'] = true;
matrix[a][b++] = tmp;
}
}
}
for (int i = 0; i < 26; i++) {
if (vis[i]) continue;
if (b == 5) {
a++;
b = 0;
if (a == 5) break;
}
char tmp = (char)('A' + i);
if (tmp == 'J') continue;
matrix[a][b++] = tmp;
}
// 记录坐标
class node {
int col, row;
node(){}
node(int col, int row) {
this.col = col;
this.row = row;
}
}
HashMap<Character, node> map = new HashMap<>();
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
// 找每个字母的位置
map.put(matrix[i][j], new node(i, j));
}
}
// 完成密钥矩阵的构造,开始生成密钥
len = ming.length(); // 明文长度
List<char[]> pair = new ArrayList<>();
int i = 0;
for (i = 0; i < len - 1; i++) {
char first = ming.charAt(i);
char last = ming.charAt(i + 1);
if (first == last) {
// 组内字符相同,插入Q
pair.add(new char[]{first, 'Q'});
} else {
pair.add(new char[]{first, last});
i++;
}
}
if (i == len - 1) {
// 最后一个字母还没入组
pair.add(new char[]{ming.charAt(i), 'Q'});
}
StringBuilder ans = new StringBuilder();
// 开始加密
for (char[] cur : pair) {
node n1 = map.get(cur[0]);
node n2 = map.get(cur[1]);
// 两个字母的矩阵位置
int col1 = n1.col;
int row1 = n1.row;
int col2 = n2.col;
int row2 = n2.row;
if (col1 != col2 && row1 != row2) {
// 不同行不同列
ans.append(matrix[col1][row2]);
ans.append(matrix[col2][row1]);
} else if (col1 == col2) {
// 同行
if (row1 == 4 || row2 == 4) {
// 一个在最右方
int right = Math.max(row1, row2);
ans.append(matrix[col1][0]);
ans.append(matrix[col1][right - 1]);
} else {
// 两个字母都不在最右方
int right = Math.max(row1, row2);
ans.append(matrix[col1][right]);
ans.append(matrix[col1][right + 1]);
}
} else if (row1 == row2) {
// 同列
if (col1 == 4 || col2 == 4) {
// 有一个字母在最下方
int down = Math.max(col1, col2);
ans.append(matrix[0][row1]);
ans.append(matrix[down - 1][row1]);
} else {
// 两个字母都不在最下方
int down = Math.max(col1, col2);
ans.append(matrix[down + 1][row1]);
ans.append(matrix[down][row1]);
}
}
}
writer.write(ans.toString() + "");
writer.flush();
}
}
六、简单的数学公式
小于 x 且和 x 互质的数的个数,铁数论,没得跑!互质的数的个数,那就是欧拉函数,只不过不包含x。
七、橘子的秘密
需要维护橘子的种类数、总数,总数好维护,就用树状数组即可,关键是种类数如何维护?