第十一届蓝桥杯国赛Java大学B组

最近在做真题,顺便发发题解,最近没空,很忙。

一、美丽的2

1.问题:

 

2.源代码:

package 蓝桥杯补题;
import java.util.*;
import java.io.*;
public class 国赛真题A_2020年 {
	static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Scanner cin = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		int ans = 0;
		for(int i = 1;i<=2020;i++) {
			String p = String.valueOf(i);
			if(p.contains("2")) {
				ans++;
			}
		}
		pw.println(ans);//563
		pw.flush();
    }

}

二、扩散

1.问题:

2.源代码:

package 蓝桥杯补题;
import java.util.*;
import java.io.*;
public class 国赛真题B_2020年 {
	static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
	static class stu{
		int x;
		int y;
		int t;
		public stu(int x,int y,int t) {
			this.x = x;
			this.y = y;
			this.t = t;
		}
	}
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Scanner cin = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		long ans = 0;
		int time = 2020;
		int xy[][] = {{1,0},{0,1},{-1,0},{0,-1}};
		boolean mp[][] = new boolean[7000][7000];
		LinkedList<stu> Q = new LinkedList<>();
		Q.add(new stu(2500,2500,0));
		Q.add(new stu(4520,2511,0));
		Q.add(new stu(2511,2514,0));
		Q.add(new stu(4500,4500,0));
		int answer = 0;
		while(!Q.isEmpty()) {
			stu now = Q.poll();
			if(now.t == 2021) {
				continue;
			}
			if(mp[now.x][now.y]) {
				continue;
			}
			mp[now.x][now.y] = true;
			answer++;
			for(int i = 0;i<4;i++) {
				int nx = now.x + xy[i][0];
				int ny = now.y + xy[i][1];
				if(!mp[nx][ny])
				Q.add(new stu(nx,ny,now.t+1));
			}
		}
		pw.println(answer);//20312088
		pw.flush();
	}

}

三、阶乘约数

1.问题:

 

2.源代码:

package 蓝桥杯补题;
import java.util.*;
import java.io.*;
import java.math.*;
public class 国赛真题C_2020年 {
	private static BigInteger one =  new BigInteger("1");
	private static BigInteger two =  new BigInteger("2");
	static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
	private static BigInteger find(BigInteger num) {
		BigInteger ans = one;
		for(BigInteger i = two; i.multiply(i).compareTo(num)<=0; i = i.add(one)) {
			BigInteger j = one;
			while(num.mod(i).equals(BigInteger.ZERO)) {
				j = j.add(one);
				num = num.divide(i);
			}
			if(j.compareTo(one) > 0)
			ans = ans.multiply(j);
		}
		if(num.compareTo(one) > 0) {
			return ans.multiply(two);
		}
		else {
			return ans;
		}
	}
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Scanner cin = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		BigInteger ans = one;
		for(int i = 1;i<=100;i++) {
			ans = ans.multiply(new BigInteger(String.valueOf(i)));
		}
		BigInteger answer = find(ans);
		pw.print(answer);//39001250856960000
		pw.flush();
	}

}

四、本质上升序列

1.问题:

 

2.源代码:

package 蓝桥杯补题;
import java.util.*;
import java.io.*;
import java.math.*;
public class 国赛真题D_2020年 {
	private static class stu{
		char str;
		int pi;
		public stu(char str, int pi) {
			this.str = str;
			this.pi = pi;
		}
	}
	static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Scanner cin = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		String s = cin.next();
		int n = s.length();
		long mp[] = new long [n];
		boolean ans[] = new boolean[1006];
		for(int i = n-1;i >= 0;i--) {
			mp[i] = 1;
			Arrays.fill(ans, false);
			for(int j = i+1; j< n;j++) {
				if(s.charAt(j) > s.charAt(i) && ans[s.charAt(j)] == false) {
					mp[i]+=mp[j];
					ans[s.charAt(j)] = true;
				}
			}
		}
		long answer = 0;
		Arrays.fill(ans, false);
		for(int i = 0;i<n;i++) {
			if(!ans[s.charAt(i)]) {
				answer+=mp[i];
				ans[s.charAt(i)] = true;
			}
		}
		pw.println(answer);//3616159
		pw.flush();
	}
//tocyjkdzcieoiodfpbgcncsrjbhmugdnojjddhllnofawllbhfiadgdcdjstemphmnjihecoapdjjrprrqnhgccevdarufmliqijgihhfgdcmxvicfauachlifhafpdccfseflcdgjncadfclvfmadvrnaaahahndsikzssoywakgnfjjaihtniptwoulxbaeqkqhfwl
}

五、玩具蛇

1.问题:

2.源代码:

package 蓝桥杯补题;
import java.util.*;
import java.io.*;
import java.math.*;
public class 国赛真题E_2020年 {
	static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
	static int answer = 0;
	static boolean mp[][] = new boolean[5][5];
	static int xy[][] = {{1,0},{0,1},{-1,0},{0,-1}};
	private static void dfs(int x,int y,int value) {
		if(value == 16) {
			answer++;
			return;
		}
		for(int i = 0;i<4;i++) {
			int nx = x + xy[i][0];
			int ny = y + xy[i][1];
			if(nx >= 1 && nx <= 4 && ny >= 1 && ny <= 4 && !mp[nx][ny] ) {
				mp[nx][ny] = true;
				dfs(nx,ny,value+1);
				mp[nx][ny] = false;
			}
		}
	}
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Scanner cin = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		
		for(int i = 1;i<=4;i++) {
			for(int j = 1;j<=4;j++) {
				for(boolean as[] : mp) {
					Arrays.fill(as, false);
				}
				mp[i][j] = true;
				dfs(i,j,1);
			}
		}
		pw.println(answer);//552
		pw.flush();
		
	}

}

六、蓝肽子序列

1.问题:

 

 

2.源代码:

package 蓝桥杯补题;
import java.util.*;
import java.io.*;
import java.math.*;
public class 国赛真题F_2020 {
	static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Scanner cin = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		String p1 = cin.next();
		String p2 = cin.next();
		String s1[] = new String[1001];
		String s2[] = new String[1001];
		Arrays.fill(s1, "");
		Arrays.fill(s2, "");
		int pi1 = 0;
		for(int i = 0;i<p1.length();) {
			pi1++;
			if(p1.charAt(i) <= 'Z' && p1.charAt(i) >= 'A') {
				s1[pi1]+=p1.charAt(i);
				i++;
				while(i<p1.length() && p1.charAt(i) <= 'z' && p1.charAt(i) >= 'a') {
					s1[pi1]+=p1.charAt(i);
					i++;
				}
			}
			
		}
		int pi2 = 0;
		for(int i = 0;i<p2.length();) {
			pi2++;
			if(p2.charAt(i) <= 'Z' && p2.charAt(i) >= 'A') {
				s2[pi2]+=p2.charAt(i);
				i++;
				while(i<p2.length() && p2.charAt(i) <= 'z' && p2.charAt(i) >= 'a') {
					s2[pi2]+=p2.charAt(i);
					i++;
				}
			}
			
		}
		int mp[][] = new int[pi1+1][pi2+1];
		for(int i = 1;i<=pi1;i++) {
			for(int j = 1;j<=pi2;j++) {
				if(s1[i].equals(s2[j])) {
					mp[i][j] = mp[i-1][j-1]+1; 
				}
				else {
					mp[i][j] = Math.max(mp[i][j-1],mp[i-1][j]);
				}
			}
		}
		pw.print(mp[pi1][pi2]);
		pw.flush();
	}
}

 

七、皮亚诺曲线距离

1.问题:

 

 

2.源代码:

package 蓝桥杯补题;
import java.util.*;
import java.io.*;
import java.math.*;
public class 国赛真题G_2020年 {
	static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
	static BigInteger mp[] = new BigInteger[301];
	static BigInteger one = new BigInteger("1");
	static BigInteger three = new BigInteger("3");
	static BigInteger two = new BigInteger("2");
	
	private static void init() {
		mp[0] = one;
		for(int i = 1;i<=200;i++) {
			mp[i] = mp[i-1].multiply(three);
		}
	}
	private static BigInteger find(int n,BigInteger x,BigInteger y) {
		if(n == 0) {
			return one;
		}
		BigInteger num = mp[n];
		BigInteger add = mp[n*2].divide(new BigInteger("9"));
		if(x.compareTo(num.divide(three))<0) {
			if(y.compareTo(num.divide(three)) < 0) {
				return find(n-1,x,y);
			}
			if(y.compareTo(num.multiply(two).divide(three)) < 0) {
				return add.add(find(n-1,num.divide(three).subtract(x).subtract(one),y.subtract(num.divide(three))));
			}
			else {
				return add.multiply(two).add(find(n-1,x,y.subtract(num.multiply(two).divide(three))));
			}
		}
		else if(x.compareTo(num.multiply(two).divide(three)) < 0) {
			if(y.compareTo(num.divide(three)) < 0) {
				return add.multiply(new BigInteger("5")).add(find(n-1,x.subtract(num.divide(three)),num.divide(three).subtract(y).subtract(one)));
			}
			else if(y.compareTo(num.multiply(two).divide(three)) < 0) {
				return add.multiply(new BigInteger("4")).add(find(n-1,num.multiply(two).divide(three).subtract(one).subtract(x),num.multiply(two).divide(three).subtract(one).subtract(y)));
						
			}
			else {
				return add.multiply(three).add(find(n-1,x.subtract(num.divide(three)),num.subtract(y).subtract(one)));
			}
		}
		else {
			if(y.compareTo(num.divide(three)) < 0) {
				return add.multiply(new BigInteger("6")).add(find(n-1,x.subtract(num.multiply(two).divide(three)),y));
			}
			else if(y.compareTo(num.multiply(two).divide(three)) < 0) {
				return add.multiply(new BigInteger("7")).add(find(n-1,num.subtract(x).subtract(one),y.subtract(num.divide(three))));
			}
			else {
				return add.multiply(new BigInteger("8")).add(find(n-1,x.subtract(num.multiply(two).divide(three)),y.subtract(num.multiply(two).divide(three))));
			}
		}
	}
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Scanner cin = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		init();
		BigInteger niu = cin.nextBigInteger();
		int k = Integer.parseInt(niu.toString());
		BigInteger x1 = cin.nextBigInteger();
		BigInteger y1 = cin.nextBigInteger();
		BigInteger x2 = cin.nextBigInteger();
		BigInteger y2 = cin.nextBigInteger();
		BigInteger ans1 = find(k,x1,y1);
		BigInteger ans2 = find(k,x2,y2);
		pw.println(ans2.subtract(ans1).abs());
		pw.flush();
	}
}

 

八、画廊

1.问题:

 

 

2.源代码:

package 蓝桥杯补题;
import java.util.*;
import java.io.*;
import java.math.*;
public class 国赛真题H_2020年 {
	private static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
	private static double find(double x1,double y1,double x2,double y2) {
		return Math.sqrt((x1 - x2)*(x1 - x2)+(y1 - y2)*(y1 - y2));
	}
	private static int L,R,len,wid;
	public static void main(String[] args) {
		Scanner cin = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		L = cin.nextInt();
		R = cin.nextInt();
		int left[] = new int[L+1];
		int right[] = new int[R+1];
		len = cin.nextInt();
		wid = cin.nextInt();
		double dp[][][] = new double[L+1][R+1][2];//L左边已经搞好的,R右边已经搞好的,2种状态,左、右
		for(double as[][] : dp) {
			for(double aa[] : as)
			Arrays.fill(aa, 99999999);
		}
		for(int i = 1;i<=L;i++) {
			left[i] = cin.nextInt();
		}
		for(int i = 1;i<=R;i++) {
			right[i] = cin.nextInt();
		}
		double mid = wid*1.0/2;
		dp[1][0][0] = find(mid,0,0,left[1]);
		dp[0][1][1] = find(mid,0,wid,right[1]);
		for(int i = 2;i<=L;i++) {
			dp[i][0][0] = dp[i-1][0][0] + left[i] - left[i-1];
		}
		for(int i = 2;i<=R;i++) {
			dp[0][i][1] = dp[0][i-1][1] + right[i] - right[i-1];
		}
		dp[1][1][0] = dp[0][1][1] + find(0,left[1],wid,right[1]);
		dp[1][1][1] = dp[1][0][0] + find(0,left[1],wid,right[1]);
		for(int i = 1;i<=L;i++) {
			for(int j = 1;j<=R;j++) {
				if(i == 1 && j == 1) {
					continue;
				}
				for(int k = 1;k>=0;k--) {
					if(k == 0) {
						dp[i][j][0] = Math.min(dp[i-1][j][0] + left[i] - left[i-1], dp[i-1][j][1]+find(0,left[i],wid,right[j]));
					}
					else {
						dp[i][j][1] = Math.min(dp[i][j-1][0] +find(0,left[i],wid,right[j]), dp[i][j-1][1]+right[j] - right[j-1]);
					}
				}
;			}
		}
		double ans1 = dp[L][R][0] + find(0,left[L],mid,len);
		double ans2 = dp[L][R][1] + find(wid,right[R],mid,len);
		pw.printf("%.2f",Math.min(ans1, ans2));
		pw.flush();
	}

}

 

九、补给

1.问题:

 

 

2.源代码:

package 蓝桥杯补题;
import java.util.*;
import java.io.*;
import java.math.*;
public class 国赛真题I_2020年 {
	private static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
	private static class point {
		int x;
		int y;
		public point(int x,int y) {
			this.x = x;
			this.y = y;
		}
	}
	private static double find(point o1,point o2) {
		return Math.sqrt((o1.x - o2.x)*(o1.x - o2.x)+(o1.y - o2.y)*(o1.y - o2.y));
	}
	private static double inf = 1e9;
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Scanner cin = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		int n,m;
		n = cin.nextInt();
		m = cin.nextInt();
		point mp[] = new point[n];
		double floyd[][] = new double[n][n];
		double dp[][] = new double[1<<n][n];
		for(int i = 0;i < n ;i++) {
			mp[i] = new point(cin.nextInt(),cin.nextInt());
		}
		for(int i = 0 ;i< n ;i++) {
			for(int j = 0;j < n;j++) {
				floyd[i][j] = find(mp[i],mp[j]);
				if(floyd[i][j] > m) {
					floyd[i][j] = inf;
				}
			}
		}
		for(int k = 0;k<n;k++) {
			for(int i = 0;i<n;i++) {
				for(int j = 0;j<n;j++) {
					floyd[i][j] = Math.min(floyd[i][j], floyd[i][k] + floyd[k][j]);
				}
			}
		}
		for(double as[] : dp) {
			Arrays.fill(as, inf);
		}
		dp[1][0] = 0;
		for(int i = 0;i<(1<<n);i++) {
			for(int j = 0;j<n;j++) {
				if(((i >> j)&1) > 0) {
					for(int k = 0;k<n;k++) {
						if((((i - (1<<j))>>k)&1)>0) {
							dp[i][j] = Math.min(dp[i][j],dp[i - (1<<j)][k]+floyd[k][j]);
						}
					}
				}
			}
		}
		double ans = inf;
		for(int i = 1;i<n;i++) {
			ans = Math.min(ans, floyd[i][0] + dp[(1<<n)-1][i]);
		}
		pw.printf("%.2f",ans);
		pw.flush();
	}

}

十、质数行者 

1.问题:

  

2.源代码:

package 蓝桥杯补题;
import java.util.*;
import java.io.*;
import java.math.*;
public class 国赛真题J_2020年 {
	private static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
	private static int n,m,w,r1,h1,t1,r2,h2,t2,ans = 0,mod;
	private static boolean is[];
	private static void init() {
		mod = 1000000007;
		is = new boolean[1001];
		is[1] = true;
		is[0] = true;
		for(int i = 2;i<=1000;i++) {
			if(!is[i]) {
				for(int j = 2;i*j<=1000;j++) {
					is[i*j] = true;
				}
			}
		}
	}
	private static void dfs(int x,int y,int t) {
		if(x == n && y == m && t == w) {
			ans++;
			return ;
		}
		if(x == r1 && y == h1 && t == t1 || x == r2 && y == h2 && t == t2 ) {
			return ;
		}
		if(x > n || y > m || t > w) {
			return ;
		}
		int nx = x,ny = y,nt = t;
		for(int i = 0;i<n;i++) {
			if(!is[i]) {
				dfs(x+i,y,t);
			}
		}
		for(int i = 0;i<m;i++) {
			if(!is[i]) {
				dfs(x,y+i,t);
			}
		}
		for(int i = 0;i<w;i++) {
			if(!is[i]) {
				dfs(x,y,t+i);
			}
		}
	}
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Scanner cin = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		init();
		n = cin.nextInt();
		m = cin.nextInt();
		w = cin.nextInt();
		r1 = cin.nextInt();
		h1 = cin.nextInt();
		t1 = cin.nextInt();
		r2 = cin.nextInt();
		h2 = cin.nextInt();
		t2 = cin.nextInt();
//		dfs(1,1,1); //嘎嘎超时
		int Max = Math.max(n, Math.max(m,w));
		int dp[][][] = new int[300][300][300];
		dp[1][1][1] = 1;
		for(int i = 1;i<=n;i++) {
			for(int j = 1;j<=m;j++) {
				for(int k = 1;k<=w;k++) {
					if(i == r1 && j == h1 && k == t1 || i == r2 && j == h2 && k == t2) {
						dp[i][j][k] = 0;
					}
					for(int add = 2;add<= Max;add++) {
						if(!is[add]) {
							if(i+add<=n) {
								dp[i+add][j][k] = (dp[i+add][j][k]%mod + dp[i][j][k]%mod)%mod;
							}
							if(j+add<=m) {
								dp[i][j+add][k] = (dp[i][j+add][k]%mod + dp[i][j][k]%mod)%mod;
							}
							if(k+add<=w) {
								dp[i][j][k+add] = (dp[i][j][k+add]%mod + dp[i][j][k]%mod)%mod;
							}
						}
					}
				}
			}
		}
		ans = dp[n][m][w];
		pw.println(ans);
		pw.flush();
	}

}

用DP只有40分,没啥想法 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

stu_kk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值