当前位置:网站首页>蓝桥杯31天冲刺 Day16
蓝桥杯31天冲刺 Day16
2022-04-22 05:38:00 【柴可拉夫斯基】
金币
链接: 金币.

给定的范围最大只有10^4,直接按题目模拟就好
import java.util.*;
public class 金币 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
int n = sc.nextInt();
int sum=0;
int k =1;
int i=1;
while(i<=n) {
for(int j =1;j<=k;j++) {
sum+=k;
i++;
if(i>n)
break;
}
k++;
}
System.out.println(sum);
}
}
}
优秀的拆分
链接: 优秀的拆分.

这题初看题目,应该就能看出来不能拆分出来的情况,奇数一定不能拆分出来
那么,具体应该怎样拆分呢?想要输出方案,其实只要把给定数字转化成二进制就好了。然后将位数上为1的数转换为10进制就好
比如10的二进制:1010 = 8 + 2
8,2也就是所求结果
具体怎么拆分,只要了解lowbit()方法就可以很好解决 lowbit理解.
我们通过这个算法,每次都可以找到相应的二进制最后一位,循环找到最后,我们就可以得出结果
完整代码:
import java.util.*;
public class 优秀的拆分 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
List<Integer> list = new ArrayList<>();
int n = sc.nextInt();
if((n & 1) != 0) {
System.out.println(-1);
}
else {
while(n>0) {
list.add(n & -n);
n -= (n & -n);
}
for(int i = list.size()-1;i>=0;i--)
System.out.print(list.get(i)+" ");
}
}
}
}
穿越雷区
链接: 穿越雷区.

很典型的BFS算法
这里主要讲一下变量的设置
- class pos():记录x,y值(坐标)
- n:输入的矩阵大小
- dx,dy:每次坦克移动的方位
- l[][]:用来存放题目给出的雷区
- count[][]:记录到达此位置走过的最小步数
- path[][]:记录该位置是否走过
只要掌握了BFS的基本套路(while循环+queue),结合变量的解释,应该很容易就可以弄懂一下代码
整体代码:
import java.util.*;
public class 穿越雷区 {
public static pos sta ;
public static pos end ;
public static int n;
public static int[] dx = {
1,0,-1,0} ;
public static int[] dy = {
0,1,0,-1} ;
public static int[][] l;
public static int[][] count = new int[110][110];//记录走到此处的步数
public static int[][] path = new int[110][110];//记录此处是否走过
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
l = new int[n][n];
for(int i =0;i<n;i++) {
for(int j=0;j<n;j++) {
l[i][j] = sc.next().charAt(0);
if(l[i][j] == 'A') {
sta=new pos(i,j);
}
if(l[i][j] == 'B') {
end=new pos(i,j);
}
}
}
BFS();
System.out.println(count[end.x][end.y]);
}
public static void BFS() {
Queue<pos> queue = new LinkedList<>();
count[sta.x][sta.y]=0;
path[sta.x][sta.y]=1;
queue.add(sta);
while(!queue.isEmpty()) {
pos i = queue.poll();
for(int j =0;j<4;j++) {
int x =dx[j] + i.x;
int y =dy[j] + i.y;
if (x >= 0 && x < n && y >= 0 && y < n && l[x][y] != l[i.x][i.y] && path[x][y]!=1) {
if(count[x][y]!=0)
count[x][y]=Math.min(count[x][y], count[i.x][i.y]+1);
else
count[x][y]=count[i.x][i.y]+1;
path[x][y]=1;
queue.add(new pos(x,y));
}
}
}
}
public static class pos{
public int x,y;
public pos(int x,int y) {
this.x=x;
this.y=y;
}
public pos() {
}
}
}
版权声明
本文为[柴可拉夫斯基]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_53421929/article/details/123695779
边栏推荐
猜你喜欢
随机推荐
LeetCode 面试题 17.09. 第 k 个数--动态规划
供应链服务术语
夜神模拟器:adb命令
树莓派4B ssh connection refused
Exploration des données - regroupement
Machine learning -- drawing P-R curve and ROC curve with iris data set
Raspberry pie 4B SSH connection refused
不用第三个变量交换两变量值的几种方式
根源:pip终端下载的包import不能用
雷达设备(贪心)
Dynamically create array (c6385 reading invalid data from 'a')
The breakpoint will not currently be hit No symbols have been loaded for this document.
【2022阿里安全】真实场景篡改图像检测挑战赛 决赛rank17方案分享
Eight queens problem (backtracking method, solving N Queens at the same time)
Digital DP (template)
蓝桥杯31天冲刺 Day23
C语言--经典100题
提高工作效率的利器
vscode cmake-tools launching.json 使用文档(垃圾翻译)
蓝桥杯冲刺——二进制枚举









