The 7th Blue Bridge Cup Individual Competition Provincial Competition (Software) C / C ++ University Group A

All the code related to the Blue Bridge Cup in this blog is implemented using java.
Because the current Blue Bridge Cup does not fill in the blanks for the program, the program fills in the blanks and skips the
question. The website for my question is http://oj.hzjingma.com/site There are quite a few Blue Bridge Cup questions in / index. If you don't post the questions, you can go to the inside. If you have any questions, please comment. Other real questions will slowly update the
first question.

试题A:网友年龄 3'
某君新认识一网友。
当问及年龄时,他的网友说:
“我的年龄是个2位数,我比儿子大27,
如果把我的年龄的两位数字交换位置,刚好就是我儿子的年龄”
请你计算:网友的年龄一共有多少种可能情况?
提示:30岁就是其中一种可能哦.
请填写表示可能情况的种数。

There is nothing to think about directly writing this question

package lan7A;

public class p7000 {
	public static void main(String args[]){
		int count = 0;
		for(int i = 10;i < 100;i++){
			int a = i/10;
			int b = i%10;
			if(i == b*10+a+27){
				System.out.println(i);
				count++;
			}
		}
		System.out.println(count);
	}
}

The second question test question B: birthday candle 5 '

package lan7A;
public class P7001 {
	public static void main(String[] args) {
//		int n = 0;
//		for(int i = 1;i < 100;i++){ // 开始年龄
//			n = 0;
//			for(int j = i;j < 150;j++){
//				n += j;
//				if(n == 236)
					System.out.println(26);
//			}
//		}
	}
}

Exam question C: Fill in the square 11 '

图片我就不插了- - 有点麻烦
如下的10个格子
填入0~9的数字。要求:连续的两个数字不能相邻。
(左右、上下、对角都算相邻)
一共有多少种可能的填数方案?
请填写表示方案数目的整数。

This question is very obvious. At first glance, it is the form of search. We can use dfs to write, start from the second grid in the first row and fill in the numbers one by one, and fill in the fourth in the third row to succeed ans ++
(1) Use vis to indicate whether this number has been used
(2) Use the get function to determine whether this number is filled here.

package lan7A;
public class P7002 {
	static boolean vis[];
	static int ans = 0;
	static int map[][];
	static boolean get(int x,int y,int k){
		for(int i = -1;i <= 1;i++)
			for(int j = -1;j <= 1;j++){
				if(i == 0 && j== 0)
					continue;
				if(x+i>=1 && x+i <=3 && y+j>=1 && y+j <=4)
					if(Math.abs(map[x+i][y+j]-k) == 1)
						return false;
			}
		return true;
	}
	static void dfs(int x,int y){
		if(x==3&&y==4){ // 到最后一个格子了
			ans++;
			return;
		}
		if(y>4){
			dfs(x+1,1);// 已经大于4了说明一行填完了,填下一行
			return;
		}
		for(int i = 0;i < 10;i++){
			if(!vis[i] && get(x,y,i)){
				map[x][y] = i;
				vis[i] = true;
				dfs(x,y+1);
				vis[i] = false;
				map[x][y] = -10; // 注意题意的是否可以填这个数字的意思是跟别的数不连续,所以我们最好弄个负数,跟所有数都不连续,表示没使用过
			}
		}
	}
	public static void main(String args[]){
		vis = new boolean[10];
		map = new int[4][5];
		for(int i = 0;i < 4;i++)
			for(int j = 0;j < 5;j++)
				map[i][j] = -10; // 跟上面dfs中一样的道理啦
		dfs(1,2);
		System.out.println(ans);
	}
}

Test Question F: Winter Holiday Homework 15 '

这个也有图片。。我就不贴题目了

The idea of ​​this question is full arrangement
(1) The point to pay attention to is the division. If we use integers, we can not only judge whether it satisfies the division, but also ensure that the modular division is 0. After all, 4/3 = 1; the
full arrangement template, If you need it, you can store it. The Blue Bridge Cup should be very useful.

package lan7A;
public class P7005 {
	static int a[];
	static int ans;
	static void dfs(int l,int r){
		if(l == r){
			if(a[0]+a[1]==a[2]&&a[3]-a[4]==a[5]&&a[6]*a[7]==a[8]&&a[9]/a[10]==a[11]&&a[9]%a[10]==0){
				ans++;
				return;
			}
		}
		for(int i = l;i <= r;i++){
			int t;
			t = a[l];
			a[l] = a[i];
			a[i] = t;
			dfs(l+1,r);
			t = a[l];
			a[l] = a[i];
			a[i] = t;
		}
	}
	public static void main(String[] args) {
		a = new int[]{1,2,3,4,5,6,7,8,9,10,11,12,13};
		dfs(0,12);
		System.out.println(ans);
	}
}

Question G: Cut the stamp 19 '

如图1,12张连在一起的12生肖的邮票。

现在你要从中剪下5张来,要求必须是连着的。

(仅仅连接一个角不算相连)

比如,图2和图3中,粉红色所示部分就是合格的剪取。

请你计算,一共有多少种不同的剪取方法。

请填写表示方案数目的整数。

This question can be combined with a template and a connected block template, but you must not directly ask for dfs, because dfs only chooses one direction at a time, and the minus grid is different. (Because the blogger is too lazy, I really do n’t want to put the picture), in short, the case where dfs ca n’t find the third picture, because dfs will only go in one direction, this picture has two directions.

package lan7A;
public class P7006 {
	static int ans;
	static boolean map[][];
	static int xx[] = new int[]{1,-1,0,0};
	static int yy[] = new int[]{0,0,-1,1};
	static Node node[] = new Node[12];
	static int b[] = new int[5];
	static Node key[] = new Node[5]; // 保存每次选的组合
	static void dfs(int x,int y,int count){
		if(x<0 || x>=3 || y<0 || y>=4)
			return;
		if(map[x][y])
			return;
		map[x][y] = true;
		for(int i = 0;i < 4;i++){
			int dx = x+xx[i];
			int dy = y+yy[i];
			dfs(dx,dy,count);
		}
	}
	static void search(int m,int n){ // 组合模板
		for(int i = m;i <= n;i++){
			b[m-1] = i-1;
			if(m > 1)
				search(m-1,i-1);
			else{
				for(int j = 0;j < 3;j++) // 初始化地图 便于求联通块数量
					for(int k = 0;k < 4;k++)
						map[j][k] = true;
				for(int j = 0;j < 5;j++){ // 将我们选出来的点存到key中
					key[j] = new Node();
					key[j].x = node[b[j]].x;
					key[j].y = node[b[j]].y;
					map[key[j].x][key[j].y] = false;
				}
				int count = 0;
				for(int k = 0;k < 5;k++) // 判断我们组合的5个点有几个连通块
					if(!map[key[k].x][key[k].y])
						 dfs(key[k].x,key[k].y,++count);
				if(count == 1){ // 等于1说明只有1个连通块
//					for(int k = 0;k < 5;k++)
//						System.out.print(key[k].x+" "+key[k].y+" ");
//					System.out.println();
					ans++;
				}
			}
		}
	}
	public static void main(String[] args) {
		map = new boolean[4][4];
		int k = 0;
		for(int i = 0;i < 3;i++)
			for(int j = 0;j < 4;j++){
				node[k] = new Node();
				node[k].x = i;
				node[k++].y = j;
				map[i][j] = false;
			}
		search(5,12); // 12个格子中任取5个
		System.out.println(ans);
	}
}
class Node{
	int x;
	int y;
}

Test Question H: Four Square Sum 21 '

四平方和定理,又称为拉格朗日定理:

每个正整数都可以表示为至多4个正整数的平方和。

如果把0包括进去,就正好可以表示为4个数的平方和。

对于一个给定的正整数,可能存在多种平方和的表示法。

要求你对44个数排序:

0 <= a <= b <= c <= d 

并对所有的可能表示法按 a,b,c,da,b,c,d 为联合主键升序排列,最后输出第一个表示法

A direct violence, of course, find a way to optimize N before violence. The maximum can be taken as 5 * 1e6. Direct 4-fold loop is certainly not acceptable
(1) It should be noted that because it is a sum of squares, we only need to enumerate to sqrt (N ) It's fine
(2) 4 square sums, we just enumerate 3, just the last one can be subtracted directly with N and then find the square root and then determine whether the square sum is equal

package lan7A;
import java.util.*;
public class P7007 {
	static void get(int x){
		for(int h1 = 0;h1*h1<=x;h1++){
			for(int h2 = h1;h2*h2<=x;h2++){
				for(int h3 = h2;h3*h3<=x;h3++){
					int h4 = (int)Math.sqrt(x-h1*h1-h2*h2-h3*h3);
						if(h1*h1+h2*h2+h3*h3+h4*h4 == x){
							System.out.println(h1+" "+h2+" "+h3+" "+h4);
							return;
					}
				}
			}
		}
	}
	public static void main(String args[]){
		Scanner sc = new Scanner(System.in);
		int n =sc.nextInt();
		get(n);
	}
}

Published 32 original articles · praised 5 · visits 862

Guess you like

Origin blog.csdn.net/shizhuba/article/details/104885666