文章目录
纯质数
题目链接:https://www.lanqiao.cn/problems/1561/learning/
package daily;
import java.util.ArrayList;
import java.util.Arrays;
public class day3_8_1 {
static int N = 20210605;
public static boolean[] nums = new boolean[N + 1];// 用来记录每一位是不是质数
public static void main(String[] args) {
int ans = 0;
Arrays.fill(nums, true);
nums[0] = false;
ArrayList<Integer> list = new ArrayList<>();// 存下所有的纯质数,由于自己查看
for (int i = 2; i < nums.length; i++) {
if (nums[i] == true) {
// 将是该质数正数倍的全部过滤掉
int j = i;
while (j < nums.length) {
nums[j] = false;
j = j + i;
}
// 判断该数是不是纯质数
if (isPurePrime(i)) {
list.add(i);
ans++;
}
}
}
// System.out.println(list); 这里只是为了看有哪些数,提交时候需要注释掉
System.out.println(ans);
}
/**
* 判断一个数字是不是纯质数,只要逐位判断是不是质数就可以了
*
* @param i
* @return
*/
private static boolean isPurePrime(int i) {
while (i > 0) {
int rightBit = i % 10;
if (!(rightBit == 2 || rightBit == 3 || rightBit == 5 || rightBit == 7)) {
return false;
}
i = i / 10;
}
return true;
}
}
最少砝码
题目链接:https://www.lanqiao.cn/problems/1461/learning/
package daily;
import java.util.Scanner;
public class day3_8_2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.close();
int range = 1; // 量程
int res = 1; // 砝码数
while (range < n) {
range += (range + range + 1);
res++;
}
System.out.println(res);
}
}
灌溉
题目链接:https://www.lanqiao.cn/problems/551/learning/
package daily;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Scanner;
public class day3_8_3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
boolean[][] farm = new boolean[n][m];// 农场大小,表示是否被灌溉过了
Deque<Node> nodeDeque = new LinkedList<>();// 创建队列用于模拟灌按照时间灌溉
int t = sc.nextInt();
while (t > 0) {
t--;
int r = sc.nextInt() - 1;
int c = sc.nextInt() - 1;
nodeDeque.addLast(new Node(r, c, 0));// 出书管的位置默认已经灌溉好了
farm[r][c] = true;// 标记已经被灌溉过了
}
int k = sc.nextInt();
sc.close();
int ans = 0;
// 每块地的四个灌溉方向,使用这个可以避免写四个if
int[][] direction = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
while (!nodeDeque.isEmpty()) {
ans++;
Node node = nodeDeque.removeFirst();
// 这里说明时间结束了,就不继续加入其临近节点了
if (node.k == k) {
continue;
}
// 遍历它的四个方向
for (int i = 0; i < direction.length; i++) {
int newR = node.r + direction[i][0];
int newC = node.c + direction[i][1];
// 新节点必须在农场范围内并且没有被灌溉过
if (newR >= 0 && newR < n && newC >= 0 && newC < m && farm[newR][newC] != true) {
// 时间是当前节点的时间+1
nodeDeque.addLast(new Node(newR, newC, node.k + 1));
}
}
}
System.out.println(ans);
}
}
class Node {
int r;// 行
int c;// 列
int k;// 灌溉到这里用的时间
public Node(int r, int c, int k) {
super();
this.r = r;
this.c = c;
this.k = k;
}
}