2017第八届蓝桥杯决赛题解报告(JavaC组)

标题:数位和
数学家高斯很小的时候就天分过人。一次老师指定的算数题目是:1+2+...+100。
高斯立即做出答案:5050!
这次你的任务是类似的。但并非是把一个个的数字加起来,而是对该数字的每一个数位作累加。
这样从1加到100的“和”是:901
从10加到15是:21,也就是:1+0+1+1+1+2+1+3+1+4+1+5,这个口算都可以出结果的。

按这样的“加法”,从1加到1000是多少呢?

public class 数位和 {
	public static int f(int a) { // 单个数的数位和
		int sum = 0;
		while(a>0) {
			sum += a%10;
			a/=10;
		}
		return sum;
	}
	public static void main(String[] args) {
		int sum =0;
		for(int i=1; i<=1000; i++) { // 1至1000的数位和
			sum += f(i);			
		}
		System.out.println(sum);
	}
}
题:数字划分
w星球的长老交给小明一个任务:
1,2,3...16 这16个数字分为两组。
要求:
这两组数字的和相同,
并且,两组数字的平方和也相同,
并且,两组数字的立方和也相同。
请你利用计算机的强大搜索能力解决这个问题。
并提交1所在的那个分组的所有数字。
这些数字要从小到大排列,两个数字间用一个空格分开。

即类似:1 4 5 8 ...  这样的答案。

题目提示了两边都为8个数字

public class 数字划分 {
	static int[] a = new int[8];
	static boolean[] b = new boolean[16];
	public static boolean ok(int[] a) {
		int sum = 0;
		for(int i=0; i<8; i++)
			sum += a[i];
		if(sum != 68) return false;  // 如果a的一般不是68则两组数字和肯定不等
		int sum1 = 0; // 第一组数据的平方和
		int sum2 = 0; // 第一组数据的立方和
		int res1 = 0; // 第二组数据的平方和
		int res2 = 0; // 第二组数据的立方和
		int x = 0;
		for(int i=1; i<=16; i++) {
			if(x<8 && a[x]==i) {
				x++;
				sum1 += i*i; 
				sum2 += i*i*i;
			}else {
				res1 += i*i;
				res2 += i*i*i;
			}
		}
		if(sum1==res1 && sum2==res2)return true; // 平方和立方和都相等
		else return false;
	}
	public static void dfs(int step, int last) {
		if(step==8) { // 只需要枚举8个就好了
			if(ok(a)) {
				// 打印结果
				for(int i=0; i<8; i++) {
					System.out.print(a[i]+" ");
				}
			}
			return;
		}
		for(int i=last+1; i<=16; i++) { // i = last+1 保证该数组为递增数组,保证了数组内的元素不重复
			a[step] = i;
			dfs(step+1, i);
		}
	}
	public static void main(String[] args) {
		int sum = 0;
		for(int i=1; i<=16; i++)
			sum += i;
		System.out.println(sum/2);
		a[0] = 1;
		dfs(1,1); // 第一个数为一,当前已经遍历了1个这个数
		// 用来检验
		int sum1 = 1*1+4*4+6*6+7*7+10*10+11*11+13*13+16*16;
		int sum2 = 1*1*1+4*4*4+6*6*6+7*7*7+10*10*10+11*11*11+13*13*13+16*16*16;
		int res1 = 2*2+3*3+5*5+8*8+9*9+12*12+14*14+15*15;
		int res2 = 2*2*2+3*3*3+5*5*5+8*8*8+9*9*9+12*12*12+14*14*14+15*15*15;
		System.out.println(sum1+" "+res1+" "+sum2+" "+res2);

	}
} 

题:树形显示

import java.util.*;

class MyTree
{
	private Map<String, List<String>>  map_ch = new HashMap<String, List<String>>();
	private Map<String,String> map_pa = new HashMap<String,String>();
	
	public void add(String parent, String child)
	{
		map_pa.put(child, parent);
		
		List<String> lst = map_ch.get(parent);
		if(lst==null){
			lst = new ArrayList<String>();
			map_ch.put(parent, lst);
		}
		lst.add(child);
	}
	
	public String get_parent(String me){
		return map_pa.get(me);
	}
	
	public List<String> get_child(String me){
		return map_ch.get(me);
	}
	
	private String space(int n)
	{
		String s = "";
		for(int i=0; i<n; i++) s += ' ';
		return s;
	}
	
	private boolean last_child(String x){
		String pa = map_pa.get(x);
		if(pa==null) return true;
		
		List<String> lst = map_ch.get(pa);
		return lst.get(lst.size()-1).equals(x);
	}
	
	public void show(String x){
		
		String s = "+--" + x;
		
		String pa = x;
		while(true){
			pa = map_pa.get(pa);
			if(pa==null) break;
			s =  last_child(pa) ? space(4) + s : "|"+ space(3) + s;  // 填空
		}
		
		System.out.println(s);
	}
	
	public void dfs(String x){
		show(x);
		
		List<String> lst = map_ch.get(x);
		if(lst==null) return;
				
		for(String it: lst){
			dfs(it);
		}
	}
}

public class 树形显示
{
	public static void main(String[] args)
	{
		MyTree tree = new MyTree();
		tree.add("root", "dog");
		tree.add("root", "cat");
		tree.add("root", "duck");
		tree.add("dog", "AAdog");
		tree.add("dog", "BBdog");
		tree.add("dog", "CCdog");
		tree.add("AAdog", "AAdog01");
		tree.add("AAdog", "AAdog02");
		tree.add("cat", "XXcat");
		tree.add("cat", "YYcat");
		tree.add("XXcat","XXcat-oo");
		tree.add("XXcat","XXcat-qq");
		tree.add("XXcat-qq", "XXcat-qq-hahah");
		tree.add("duck", "TTduck");
		tree.add("TTduck", "TTduck-001");
		tree.add("TTduck", "TTduck-002");
		tree.add("TTduck", "TTduck-003");
		tree.add("YYcat","YYcat.hello");
		tree.add("YYcat","YYcat.yes");
		tree.add("YYcat","YYcat.me");		
		
		tree.dfs("root");
	}
}

标题: 小数第n位

我们知道,整数做除法时,有时得到有限小数,有时得到无限循环小数。
如果我们把有限小数的末尾加上无限多个0,它们就有了统一的形式。
本题的任务是:在上面的约定下,求整数除法小数点后的第n位开始的3位数。
输入:
 一行三个整数:a b n,用空格分开。a是被除数,b是除数,n是所求的小数后位置(0<a,b,n<1000000000)
输出:
一行3位数字,表示:a除以b,小数后第n位开始的3位数字。
比如:
输入:
1 8 1
程序应该输出:
125
再比如:
输入:
1 8 3
程序应该输出:
500
再比如:
输入:

282866 999000 6

程序应该输出:

914

import java.util.Scanner;

public class 小数第n位 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long a = sc.nextLong();
		long b = sc.nextLong();
		long n = sc.nextLong();
		sc.close();
		long sn = n, count = 0;
		long sa = a % b; // 把整数余掉后面的就是小数点的了
		while(sn-- != 0) {
			if(sa == b) // 取余之后会等于0
				break;
			if(sa < b) { // 这一次取余的结果为0, 即该处的小数为0,进位
				sa *= 10;
			} else {
				sa %= b;  //  逐步求解小数
				sa *= 10;
				if(sa == 0)
					break; // 为0后跳出循环,已经求完所有小数了后面都为0了
			}
			count ++; // 第一次传进来的时候是 *10(进位),不是正式运算,可是count为1,所以每一次都比求余数目多一,所以下面在求最后一个循环数之前先剪了
			if(sa%b == a%b) { // 再次求余的时候与小数点后与第一次求余一样,开始循环,剪掉循环部分
				sn = n%count;
			}
		}
		if(sa == 0) {
			System.out.println("000");
		} else {
			int i = 3;
			while(i-- != 0) { 
				System.out.print(sa/b);  // 逐步输出n后的每一位
				sa %= b; 				 // 余=表示除去之后剩余的
				sa *= 10; 				 // *10表示进位
			}
		}
	}
}
标题:分考场


n个人参加某项特殊考试。
为了公平,要求任何两个认识的人不能分在同一个考场。
求是少需要分几个考场才能满足条件。


输入格式:
第一行,一个整数n(1<n<100),表示参加考试的人数。
第二行,一个整数m,表示接下来有m行数据
以下m行每行的格式为:两个整数a,b,用空格分开 (1<=a,b<=n)  表示第a个人与第b个人认识。


输出格式:
一行一个整数,表示最少分几个考场。


例如:
输入:
5
8
1 2
1 3
1 4
2 3
2 4
2 5
3 4
4 5


程序应该输出:
4


再比如:
输入:
5
10
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5


则程序应该输出:

5

import java.util.Scanner;

public class 分考场 {
	static int n;
	static int m;
	static boolean[][] con = new boolean[150][150];; // 关系
	static int[][] room = new int[150][150]; // 房间号房间里的学生
	static int[] cnt = new int[150]; // 学生的数量
	static int min = Integer.MAX_VALUE/2;
	public static void dfs(int num, int id) { // num 当前房间号, id 当前学生编号
		if(num >= min) return;
		if(id > n) {
			min = min < num ? min : num;
			return;
		}
		for(int i=1; i<=num; i++) {  // 从一号房间到当前房间,遍历已经开过的房间,检查学生id是否能进入房间,依次尝试进入能进入的房间,找到最优解
			int sz = cnt[i];		 // i号房间里的人数
			int jishu = 0; // 该房间与id不认识人数
			for(int j=1; j<=sz; j++) { // i号房间里的第j个学生
				if(!con[id][room[i][j]]) // 没关系
					jishu++;
			}
			if(jishu == sz) { // id 与i号房间的学生都没关系
				room[i][++cnt[i]] = id; // i号房间学生增加
				dfs(num, id+1);		    
				cnt[i]--;				// 学生减少,回溯
			}
		}
		// 重开一个房间
		room[num+1][++cnt[num+1]] = id; // 放到下一个房间,学生人数加1
		dfs(num+1, id+1);
		--cnt[num+1];					// 回溯,学生人数减1
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		m = sc.nextInt();
		for(int i=0; i<m; i++) {
			int t1 = sc.nextInt();
			int t2 = sc.nextInt();
			con[t1][t2] = true;
			con[t2][t1] = true;
		}
		dfs(0,1); // 0 表示还未开房间, 1号学生首先开房
		System.out.println(min);
	}
}
标题:合根植物


w星球的一个种植园,被分成 m * n 个小格子(东西方向m行,南北方向n列)。每个格子里种了一株合根植物。
这种植物有个特点,它的根可能会沿着南北或东西方向伸展,从而与另一个格子的植物合成为一体。


如果我们告诉你哪些小格子间出现了连根现象,你能说出这个园中一共有多少株合根植物吗?


输入格式:
第一行,两个整数m,n,用空格分开,表示格子的行数、列数(1<m,n<1000)。
接下来一行,一个整数k,表示下面还有k行数据(0<k<100000)
接下来k行,第行两个整数a,b,表示编号为a的小格子和编号为b的小格子合根了。


格子的编号一行一行,从上到下,从左到右编号。
比如:5 * 4 的小格子,编号:
1  2  3  4
5  6  7  8
9  10 11 12
13 14 15 16
17 18 19 20


样例输入:
5 4
16
2 3
1 5
5 9
4 8
7 8
9 10
10 11
11 12
10 14
12 16
14 18
17 18
15 19
19 20
9 13
13 17




样例输出:

5

import java.util.Arrays;
import java.util.Scanner;

public class 合根植物 {
	static int[] id;
	static int r = 0;
	public static int find(int p) { // 查找父节点
		int root = p;
		while(id[root] > 0) root = id[root];
		int k = p, i;
		while(id[k]>0) {
			i = id[k];
			id[k] = root;
			k = i;
		}
		return root;
	}
	public static void union(int a, int b) { // 联通
		int rootA = find(a);
		int rootB = find(b);
		if(rootA == rootB) return;
		int sum = id[rootA] + id[rootB];
		if(id[rootA]<id[rootB]) {
			id[rootB] = rootA;
			id[rootA] = sum;
		}else {
			id[rootA] = rootB;
			id[rootB] = sum;
		}
		r++; // 联通次数+1
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int m = sc.nextInt();
		int n = sc.nextInt();
		int k = sc.nextInt();
		id = new int[n*m+1];
		Arrays.fill(id, -1);
		for(int i=1; i<=k; i++) {
			int t1 = sc.nextInt();
			int t2 = sc.nextInt();
			union(t1,t2);
		}
		System.out.println(n*m-r); // 减去联通的
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_39778570/article/details/80529266