蓝桥杯31天冲刺 Day16

蓝桥杯31天冲刺 Day16

金币

链接: 金币.
在这里插入图片描述
给定的范围最大只有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() {
			
		}
	}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值