首页 > 基础资料 博客日记
第十五届蓝桥杯 模拟赛第二期java组题解
2023-12-26 21:30:29基础资料围观198次
import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
BigInteger a = new BigInteger("2");
BigInteger b = new BigInteger("2023");
BigInteger c = new BigInteger("1000");
BigInteger result = a.modPow(b, c);
System.out.println(result);
}
}
public class Main {
static int jinzhi(int i, int p) {
int sum = 0;
while (i != 0) {
sum += (i % p);
i /= p;
}
return sum;
}
public static void main(String[] args) {
int i = 1;
int count = 0;
while (true) {
if (jinzhi(i, 2) == jinzhi(i, 8)) {
count++;
}
if (count == 23) {
System.out.println(i);
break;
}
i++;
}
}
}
答案:4169
String str = "393353 901440 123481 850930 423154 240461\n" +
"373746 232926 396677 486579 744860 468782\n" +
"941389 777714 992588 343292 385198 876426\n" +
"483857 241899 544851 647930 772403 109929\n" +
"882745 372491 877710 340000 659788 658675\n" +
"296521 491295 609764 718967 842000 670302";
String[] qiege = str.split("\\s+");
int[] a = new int[qiege.length];
int[] b = new int[qiege.length];
for (int i = 0; i < qiege.length; i++) {
a[i] = Integer.parseInt(qiege[i]);
}
int maxDivisorCount = 0;
int indexMaxDivisorCount = -1;
for (int i = 0; i < a.length; i++) {
int count = 0;
for (int j = 1; j <= a[i]; j++) {
if (a[i] % j == 0) {
count++;
}
}
b[i] = count;
System.out.print(b[i] + " ");
if (b[i] > maxDivisorCount) {
maxDivisorCount = b[i];
indexMaxDivisorCount = i;
}
}
System.out.println("\n约数最多的数是:" + a[indexMaxDivisorCount] + ",约数个数为:" + maxDivisorCount);
}
答案:901440
import java.util.Scanner;
public class Main {
static char[][] s = new char[30][40];
static int[][] vis = new int[30][40];
static int[] dx = {1, -1, 0, 0};
static int[] dy = {0, 0, 1, -1};
static void dfs(int x, int y) {
if ( x < 0 || x > 29 || y < 0 || y > 39 || s[x][y] != '0') {
return;
}
if (vis[x][y] == 1)
{
return ;
}
s[x][y] = '2';
vis[x][y] = 1;
for (int i = 0; i < 4; i++) {
int sx = dx[i] + x;
int sy = dy[i] + y;
dfs(sx, sy);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 30; i++) {
String input = scanner.next();
s[i] = input.toCharArray();
}
dfs(0, 0);
int ans = 0;
for (int i = 0; i < 30; i++) {
for (int j = 0; j < 40; j++) {
if (s[i][j] == '2') {
ans++;
}
}
}
System.out.println(ans);
}
}
答案:541
Scanner scanner = new Scanner(System.in);
int x = scanner.nextInt();
int temp = x / 100000; // 十万位的数字
x = (x % 100000) * 10 + temp; // 将十万位的数字变为个位,其他数字向左移动
System.out.println(x);
Scanner sc = new Scanner(System.in);
String str = sc.next();
StringBuilder sb =new StringBuilder(str);
String str1 = String.valueOf(sb.reverse());
for (int i = 0 ; i < str1.length() ;i++)
{
if (str1.charAt(i)=='a'||str1.charAt(i)=='e'||str1.charAt(i)=='i'||str1.charAt(i)=='o'||str1.charAt(i)=='u')
{
System.out.println(str1.charAt(i));
break;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long n = sc.nextLong();
while (true) {
if (chengji(n) < 10) {
System.out.println(chengji(n)); // 打印结果
break;
} else {
System.out.println(chengji(n)); // 打印中间结果
n = chengji(n); // 更新n的值
}
}
}
private static long chengji(long n) {
long sum = 1; // 初始化为1,因为要计算乘积
while (n > 0) {
if (n % 10 != 0) {
sum *= (n % 10);
}
n /= 10;
}
return sum;
}
import java.util.*;
public class Main {
// 检查给定位置是否在网格内
public static boolean isValid(int r, int c, int n, int m) {
return r >= 0 && r < n && c >= 0 && c < m;
}
// 求两个数的最大公约数
public static int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt(); // 行数
int m = scanner.nextInt(); // 列数
int[][] grid = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
grid[i][j] = scanner.nextInt();
}
}
int r = scanner.nextInt(); // 起始行号
int c = scanner.nextInt(); // 起始列号
int count = bfs(grid, r, c, n, m); // 使用BFS计算可达方块数量
System.out.println(count);
}
public static int bfs(int[][] grid, int r, int c, int n, int m) {
boolean[][] visited = new boolean[n][m]; // 记录已访问的方块
Queue<int[]> queue = new LinkedList<>(); // 存储待探索的方块队列
queue.offer(new int[]{r - 1, c - 1}); // 将起始位置入队,并将其标记为已访问
visited[r - 1][c - 1] = true;
int count = 1; // 记录可达方块数量
int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; // 定义上下左右四个方向
while (!queue.isEmpty()) {
int[] curr = queue.poll(); // 出队一个方块
int row = curr[0];
int col = curr[1];
for (int i = 0; i < dirs.length; i++) {
int[] dir = dirs[i]; // 对当前方块的上下左右四个方向进行探索
int newRow = row + dir[0];
int newCol = col + dir[1];
if (isValid(newRow, newCol, n, m) && !visited[newRow][newCol]) { // 判断新方块是否在网格内且未被访问过
int num = grid[newRow][newCol];
if (gcd(num, grid[row][col]) > 1) { // 判断新方块与当前方块的最大公约数是否大于1
count++; // 可达方块数量+1
queue.offer(new int[]{newRow, newCol}); // 将新方块入队,并将其标记为已访问
visited[newRow][newCol] = true;
}
}
}
}
return count;
}
}
Scanner scanner = new Scanner(System.in);
// 读取输入
int n = scanner.nextInt();
int k = scanner.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = scanner.nextInt();
}
// 计算初始窗口和
int currentSum = 0;
for (int i = 0; i < k; i++) {
currentSum += a[i];
}
int maxSum = currentSum;
// 使用滑动窗口计算最大区间和
for (int i = k; i < n; i++) {
currentSum = currentSum + a[i] - a[i - k];
maxSum = Math.max(maxSum, currentSum);
}
// 输出结果
System.out.println(maxSum);
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
标签: