文章目录
奇数倍数
题目链接:https://www.lanqiao.cn/problems/818/learning/
package daily;
/**
* https://www.lanqiao.cn/problems/818/learning/
*
* @author Jia
*
*/
public class day3_11_1 {
public static void main(String[] args) {
int N = 2019;
while (true) {
if (judgeOdd(N)) {
// 如果找到的话直接打印然后返回
System.out.println(N);
break;
}
N += 2019;
}
}
/**
* 判断一个数字每位数是不是全部都是奇数
*
* @param n
* @return
*/
private static boolean judgeOdd(int n) {
while (n > 0) {
int num = n % 10;
if (num == 0 || num == 2 || num == 4 || num == 6 || num == 8) {
return false;
}
n /= 10;
}
return true;
}
}
第几个幸运数字
题目链接: https://www.lanqiao.cn/problems/613/learning/
package daily;
public class day3_11_2 {
public static void main(String[] args) {
long n = 59084709587505l;
int num = 0;
for (int i = 0; Math.pow(7, i) < n; i++) {
for (int j = 0; Math.pow(5, j) < n; j++) {
for (int k = 0; Math.pow(3, k) < n; k++) {
if (Math.pow(7, i) * Math.pow(5, j) * Math.pow(3, k) <= n) {
num++;
}
}
}
}
System.out.println(num - 1);// 删除三个乘方为1的情况
}
}
四平方和
题目链接:http://lx.lanqiao.cn/problem.page?gpid=T2766
package daily;
import java.util.Scanner;
/**
* http://lx.lanqiao.cn/problem.page?gpid=T2766
*
* @author Jia
*
*/
public class day3_11_3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.close();
for (int i = 0; i <= Math.sqrt(n); i++) {
for (int j = i; j <= Math.sqrt(n - i * i); j++) {
for (int k = j; k <= Math.sqrt(n - i * i - j * j); k++) {
if (Math.sqrt(n - i * i - j * j - k * k) == (int) Math.sqrt(n - i * i - j * j - k * k)) {
System.out.println(i + " " + j + " " + k + " " + (int) Math.sqrt(n - i * i - j * j - k * k));
return;
}
}
}
}
}
}
迷宫
题目链接:https://www.lanqiao.cn/problems/602/learning/
package daily;
import java.util.Deque;
import java.util.LinkedList;
// 定义的节点类
class maze_Node_bfs {
int row;
int col;
String path;// 从起点到这里走过的路径
public maze_Node_bfs(int row, int col, String path) {
super();
this.row = row;
this.col = col;
this.path = path;
}
}
public class day3_11_4_bfs {
static int[][] path = { { 0, 1 }, { -1, 0 }, { 1, 0 }, { 0, -1 } };// 四种可以走的路径,按照字典序排列好了
static String[] pathChar = { "D", "L", "R", "U" };
static boolean[][] meet = new boolean[30][50];// 判断当前路径是否已经走过了
static int rowNum = 30;
static int colNum = 50;
public static void main(String[] args) {
// 迷宫矩阵的定义在最后,太大了占地方
Deque<maze_Node_bfs> queue = new LinkedList<>();
queue.addFirst(new maze_Node_bfs(0, 0, ""));
String resultPath = null;
while (!queue.isEmpty()) {
maze_Node_bfs node = queue.removeLast();
// 判断是否已经到了出口
if (node.row == 29 && node.col == 49) {
resultPath = node.path;
break;
}
// 遍历四个方向
for (int i = 0; i < path.length; i++) {
int newCol = node.col + path[i][0];
int newRow = node.row + path[i][1];
// 判断这个方向可不可以走
if (bound(newRow, newCol) && constrain(maze, newRow, newCol)) {
meet[newRow][newCol] = true;
queue.addFirst(new maze_Node_bfs(newRow, newCol, node.path + pathChar[i]));
}
}
}
System.out.println(resultPath);
}
/**
* 判断这一步有没有走过以及是不是能走(不是墙)
*
* @param maze
* @param newRow
* @param newCol
* @return
*/
private static boolean constrain(int[][] maze, int newRow, int newCol) {
return maze[newRow][newCol] == 0 && meet[newRow][newCol] == false;
}
/**
* 判断当前这一个地方是不是在迷宫区域内
*
* @param newRow
* @param newCol
* @return
*/
private static boolean bound(int newRow, int newCol) {
return newRow < rowNum && newCol < colNum && newRow >= 0 && newCol >= 0;
}
static int[][] maze = {
{ 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0,
1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0 },
{ 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 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