zoukankan      html  css  js  c++  java
  • 2015年第六届蓝桥杯javaB组 试题 答案 解析

    1.三角形面积

    • 如图1所示。图中的所有小方格面积都是1。
    • 那么,图中的三角形面积应该是多少呢?
    • 请填写三角形的面积。不要填写任何多余内容或说明性文字。
    答案 : 28

     


    2.立方变自身

    • 观察下面的现象,某个数字的立方,按位累加仍然等于自身。
    1^3 = 1 
    8^3  = 512    5+1+2=8
    17^3 = 4913   4+9+1+3=17
    
    • 请你计算包括1,8,17在内,符合这个性质的正整数一共有多少个?

    考到一个知识点, 如何把一个整数拆分出每个位上的数. 见代码

    public class Main {
    	public static void main(String[] args) {
    		int res = 0;
    		int[] partNum = null;
    		int cubis = 0;
    		int sum = 0;
    		for(int i = 1; i <= 1000000; i++){//i从小尝试到大, 发现结果也就那6个数.
    			sum = 0;
    			cubis = i * i * i;
    			partNum = getPartNum(cubis);
    			for(int j = 0; j < partNum.length; j++){
    				sum += partNum[j];
    			}
    			if(sum == i){
    				res++;
    			}
    		}
    		System.out.println(res);
    	}
    
    	public static int[] getPartNum(int num){
    		String numStr = String.valueOf(num);
    		int[] res = new int[numStr.length()];
    		int system = 1;//用于计算10的n次方
    		for(int i = 0; i < res.length; i++){
    			res[res.length - i - 1] = num / system % 10;
    			system *= 10;
    		}
    		return res;
    	}
    }
    
    答案 : 6

     


    3.三羊献瑞

    • 观察下面的加法算式:
          祥 瑞 生 辉
      +   三 羊 献 瑞
    ----------------------------
       三 羊 生 瑞 气
    
    • 其中,相同的汉字代表相同的数字,不同的汉字代表不同的数字。
    • 请你填写“三羊献瑞”所代表的4位数字(答案唯一),不要填写任何多余内容。

    这题有个小坑, "三"字不能为0, 要不然答案不是唯一的, 其实也挺好解释的, 如果为0的话"三羊献瑞"和"三羊生瑞气"分别都不能算做一个4位数和一个5位数.

    public class T3 {
    	
    	public static void main(String[] args) {
    		for(int a = 0; a <= 9; a++){
    			for(int b = 0; b <= 9; b++){
    				if(b == a) continue;
    				for(int c = 0; c <= 9; c++){
    					if(c == a || c == b) continue;
    					for(int d = 0; d <= 9; d++){
    						if(d == a || d == b || d == c) continue;
    						for(int e = 0; e <= 9; e++){
    							if(e == a || e == b || e == c || e == d) continue;
    							for(int f = 0; f <= 9; f++){
    								if(f == a || f == b || f == c || f == d || f == e) continue;
    								for(int g = 0; g <= 9; g++){
    									if(g == a || g == b || g == c || g == d || g == e || g == f) continue;
    									for(int h = 0; h <= 9; h++){
    										if(h == a || h == b || h == c || h == d || h == e || h == f || h == g) continue;
    										
    										int num1 = a * 1000 + b * 100 + c * 10 + d;
    										int num2 = e * 1000 + f * 100 + g * 10 + b;
    										int sum = e * 10000 + f * 1000 + c * 100 + b * 10 + h;
    										if(num1 + num2 == sum && e != 0){
    											System.out.println("三:" + 1 + " 羊:" + f + " 献:" + g + " 瑞:" + 5);
    										}
    									}
    								}
    							}
    						}
    					}
    				}
    			}
    		}
    	}
    }
    
    答案 : 1085

     


    4.循环节长度

    • 两个整数做除法,有时会产生循环小数,其循环部分称为:循环节。
      比如,11/13=6=>0.846153846153..... 其循环节为[846153] 共有6位。
      下面的方法,可以求出循环节的长度。
    • 请仔细阅读代码,并填写划线部分缺少的代码。
    public static int f(int n, int m)
    	{
    		n = n % m;	
    		Vector v = new Vector();
    		
    		for(;;)
    		{
    			v.add(n);
    			n *= 10;
    			n = n % m;
    			if(n==0) return 0;
    			if(v.indexOf(n)>=0)  _________________________________ ;  //填空
    		}
    	}
    

    用Vector来记录小数点后出现的数字, 出现重复即说明开始有循环了. 但是可能没有考虑到循环的开始不是直接从小数点后开始的?

    答案 : v.size()

     


    3.九数组分数

    • 1,2,3...9 这九个数字组成一个分数,其值恰好为1/3,如何组法?
    • 下面的程序实现了该功能,请填写划线部分缺失的代码。
    public class A
    {
    	public static void test(int[] x)
    	{
    		int a = x[0]*1000 + x[1]*100 + x[2]*10 + x[3];
    		int b = x[4]*10000 + x[5]*1000 + x[6]*100 + x[7]*10 + x[8];		
    		if(a*3==b) System.out.println(a + " " + b);
    	}
    	
    	public static void f(int[] x, int k)
    	{
    		if(k>=x.length){
    			test(x);
    			return;
    		}
    		
    		for(int i=k; i<x.length; i++){
    			{int t=x[k]; x[k]=x[i]; x[i]=t;}
    			f(x,k+1);
    			_______________________________________       // 填空
    			
    		}
    	}
    	
    	public static void main(String[] args)
    	{
    		int[] x = {1,2,3,4,5,6,7,8,9};		
    		f(x,0);
    	}
    }
    

    回溯. 全排列的标准写法.

    答案 : {int t=x[k]; x[k]=x[i]; x[i]=t;}

     


    6.加法变乘法

    • 我们都知道:1+2+3+ ... + 49 = 1225
    • 现在要求你把其中两个不相邻的加号变成乘号,使得结果为2015
    • 比如:
    • 1+2+3+...+10*11+12+...+27*28+29+...+49 = 2015
    • 就是符合要求的答案。
    • 请你寻找另外一个可能的答案,并把位置靠前的那个乘号左边的数字提交(对于示例,就是提交10)。
    • 注意:需要你提交的是一个整数,不要填写任何多余的内容。

    我用了两层for循环模拟两个*号的位置, 然后原地调整数组求结果

    public class Main {
    	public static void main(String[] args) {
    		int[] arr = new int[49];
    		int num = 1;
    		for(int i = 0; i < 49; i++){//把1~49填入初始数组
    			arr[i] = num++;
    		}
    		for(int i = 0; i <= 45; i++){
    			for(int j = i + 2; j <= 47; j++){//i和j用于找到乘号的位置
    				int sum = 0;
    				for(int k = 0; k < arr.length; ){//遍历拼接算式
    					if(k == i){
    						sum += arr[k] * arr[k + 1];
    						k += 2;
    					}else if(k == j){
    						sum += arr[k] * arr[k + 1];
    						k += 2;
    					}else{
    						sum += arr[k];
    						k++;
    					}
    				}
    				if(sum == 2015)
    					System.out.println(i + 1);
    			}
    		}
    	}
    
    答案 : 16

     


    7.牌型种数

    • 小明被劫持到X赌城,被迫与其他3人玩牌。
    • 一副扑克牌(去掉大小王牌,共52张),均匀发给4个人,每个人13张。
    • 这时,小明脑子里突然冒出一个问题:
    • 如果不考虑花色,只考虑点数,也不考虑自己得到的牌的先后顺序,自己手里能拿到的初始牌型组合一共有多少种呢?

    递归的策略是: 一共要取13张牌, 选满13张结束. 每次选一种牌, 一种牌可以选0~4张. 记得回溯.

    public class T7 {
    	
    	public static int res = 0;//种类
    	
    	public static void main(String[] args) {
    		process(1, 0);
    		System.out.println(res);
    	}
    	//n是第几张牌(A~K), sum是当前的牌数.
    	public static void process(int n, int sum){
    		if(sum == 13){
    			res++;
    			return;
    		}
    		if(n < 14){
    			for(int i = 0; i < 5; i++){//每种牌可能拿0~4张
    				sum += i;
    				process(n + 1, sum);
    				sum -= i;
    			}
    		}
    	}
    }
    
    答案 : 3598180

     


    8.饮料换购

    • 乐羊羊饮料厂正在举办一次促销优惠活动。乐羊羊C型饮料,凭3个瓶盖可以再换一瓶C型饮料,并且可以一直循环下去,但不允许赊账。
    • 请你计算一下,如果小明不浪费瓶盖,尽量地参加活动,那么,对于他初始买入的n瓶饮料,最后他一共能得到多少瓶饮料。
    输入:一个整数n,表示开始购买的饮料数量(0<n<10000)
    输出:一个整数,表示实际得到的饮料数
    
    例如:
    用户输入:
    100
    程序应该输出:
    149
    
    用户输入:
    101
    程序应该输出:
    151
    
    
    资源约定:
    峰值内存消耗(含虚拟机) < 256M
    CPU消耗  < 1000ms
    
    
    请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
    

    每喝3瓶记录一次结果

    public class T8 {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		int res = 0;
    		while(n >= 3){
    			res += 3;//每次喝3瓶
    			n -= 2;//喝3瓶攒回1瓶.
    		}
    		res += n;
    		System.out.println(res);
    	}
    }
    

     


    9.垒骰子

    • 赌圣atm晚年迷恋上了垒骰子,就是把骰子一个垒在另一个上边,不能歪歪扭扭,要垒成方柱体。
    • 经过长期观察,atm 发现了稳定骰子的奥秘:有些数字的面贴着会互相排斥!
    • 我们先来规范一下骰子:1 的对面是 4,2 的对面是 5,3 的对面是 6。
    • 假设有 m 组互斥现象,每组中的那两个数字的面紧贴在一起,骰子就不能稳定的垒起来。
    • atm想计算一下有多少种不同的可能的垒骰子方式。
    • 两种垒骰子方式相同,当且仅当这两种方式中对应高度的骰子的对应数字的朝向都相同。
    • 由于方案数可能过多,请输出模 10^9 + 7 的结果。
    • 不要小看了 atm 的骰子数量哦~
    「输入格式」
    第一行两个整数 n m
    n表示骰子数目
    接下来 m 行,每行两个整数 a b ,表示 a 和 b 不能紧贴在一起。
    
    「输出格式」
    一行一个数,表示答案模 10^9 + 7 的结果。
    
    「样例输入」
    2 1
    1 2
    
    「样例输出」
    544
    
    「数据范围」
    对于 30% 的数据:n <= 5
    对于 60% 的数据:n <= 100
    对于 100% 的数据:0 < n <= 10^9, m <= 36
    
    
    资源约定:
    峰值内存消耗(含虚拟机) < 256M
    CPU消耗  < 2000ms
    

     

    小弟无才, 目前这题只想到了暴力解. 在一骰子1上面堆一个骰子2的时候, 需要知道骰子1上表面是什么数字. 当一个骰子的地面确定后, 一共有4种摆放的方式, 堆在它上面的也有4种, 一共就是4*4种, 呈指数级增长. 有待更新...

    public class Main {
    	
    	public static int res = 0;
    	public static int n = 0;
    	public static HashMap<Integer, Integer> oppositeMap = new HashMap<Integer, Integer>();//定义骰子的规则. 
    	
    	public static void main(String[] args) {
    		oppositeMap.put(1, 4);
    		oppositeMap.put(2, 5);
    		oppositeMap.put(3, 6);
    		oppositeMap.put(4, 1);
    		oppositeMap.put(5, 2);
    		oppositeMap.put(6, 3);
    		Scanner sc = new Scanner(System.in);
    		n = sc.nextInt();
    		int m = sc.nextInt();
    		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();//保存互斥的数字.
    		for(int i = 0; i < m; i++){
    			int num1 = sc.nextInt();
    			int num2 = sc.nextInt();
    			map.put(num1, num2);
    			map.put(num2, num1);
    		}
    		process(map, 1, 1);
    		System.out.println(res % 1000000007);
    	}
    	
    	public static void process(HashMap<Integer, Integer> map, int num, int lastTop){
    		if(num == n + 1){
    			res += getPowerOfFour(n);
    			return;
    		}
    		if(num == 1){//第一层骰子以哪个数字为底面都行
    			for(int i = 1; i <= 6; i++){//以某个数字作为底面
    				process(map, num + 1, oppositeMap.get(i));
    			}
    		}else{
    			int unTouch = map.containsKey(lastTop) ? map.get(lastTop) : -1;//判断是否有互斥的面.
    			if(unTouch > 0){
    				for(int i = 1; i <= 6; i++){
    					if(i != unTouch){
    						process(map, num + 1, oppositeMap.get(i));
    					}
    				}
    			}else{
    				for(int i = 1; i <= 6; i++){
    					process(map, num + 1, oppositeMap.get(i));
    				}
    			}
    		}
    	}
    	
    	public static int getPowerOfFour(int n){
    		int res = 1;
    		for(int i = 0; i < n; i++){
    			res *= 4;
    		}
    		return res;
    	}
    }
    

     


    10.生命之树

    • 在X森林里,上帝创建了生命之树。
    • 他给每棵树的每个节点(叶子也称为一个节点)上,都标了一个整数,代表这个点的和谐值。
      上帝要在这棵树内选出一个非空节点集S,使得对于S中的任意两个点a,b,都存在一个点列 {a, v1, v2, ..., vk, b} 使得这个点列中的每个点都是S里面的元素,且序列中相邻两个点间有一条边相连。
    • 在这个前提下,上帝要使得S中的点所对应的整数的和尽量大。
      这个最大的和就是上帝给生命之树的评分。
    • 经过atm的努力,他已经知道了上帝给每棵树上每个节点上的整数。但是由于 atm 不擅长计算,他不知道怎样有效的求评分。他需要你为他写一个程序来计算一棵树的分数。
    「输入格式」
    第一行一个整数 n 表示这棵树有 n 个节点。
    第二行 n 个整数,依次表示每个节点的评分。
    接下来 n-1 行,每行 2 个整数 u, v,表示存在一条 u 到 v 的边。由于这是一棵树,所以是不存在环的。
    
    「输出格式」
    输出一行一个数,表示上帝给这棵树的分数。
    
    「样例输入」
    5
    1 -2 -3 4 5
    4 2
    3 1
    1 2
    2 5
    
    「样例输出」
    8
    
    「数据范围」
    对于 30% 的数据,n <= 10
    对于 100% 的数据,0 < n <= 10^5, 每个节点的评分的绝对值不超过 10^6 。
    
    资源约定:
    峰值内存消耗(含虚拟机) < 256M
    CPU消耗  < 3000ms
    

    思路

    • 题目的意思是有一颗连通的树, 任意连通的点都可以构成一个点集, 现在的问题是如何求得一个和最大的点集.
    • 先根据样例输入画图, 画成一棵树的样子以便分析.
    • 画树的时候选取根结点是任意的.
    • 我们以一棵树分析如何求最大和点集, 假设对结点2进行分析.
    • 以结点2为源头, 向子结点方向扩充子集的范围, 最开始子集里只有结点2, 也就是-2.
    • 遍历到左孩子4, 如果以该节点为源的子集的值大于0, 那么子集就加上这个结点, 因为这样会使子集的和增大. 于是子集的和变成-2 + 4 = 2.
    • 然后继续遍历孩子结点, 把结点5也加入进来, 最终子集的和变成2 + 5 = 7.
    • 这样便确认了以2为起点的点集的最大和.
    • 通过这一逻辑, 可以推出递归函数.
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    public class T10 {
    	
    	public static long res = 0;
    	public static int n;
    	public static long[] arr;
    	public static List<Integer>[] rel;
    	
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		n = sc.nextInt();
    		arr = new long[n + 1];
    		rel = new ArrayList[n + 1];
    		initRel();
    		for(int i = 1; i < n + 1; i++){
    			arr[i] = sc.nextLong();
    		}
    		for(int i = 1; i < n; i++){
    			int a = sc.nextInt();
    			int b = sc.nextInt();
    			rel[a].add(b);
    			rel[b].add(a);
    		}
    		process(1, 0);
    		System.out.println(res);
    	}
    	
    	public static void process(int cur, int father){
    		for(int i = 0; i < rel[cur].size(); i++){
    			int child = rel[cur].get(i);
    			if(child == father) continue;
    			process(child, cur);
    			if(arr[child] > 0){
    				arr[cur] += arr[child];
    			}
    		}
    		res = Math.max(res, arr[cur]);
    	}
    	
    	public static void initRel(){
    		for(int i = 0; i <= n; i++){
    			rel[i] = new ArrayList<Integer>();
    		}
    	}
    }
    
  • 相关阅读:
    BZOJ 3668: [Noi2014]起床困难综合症【贪心】
    浅谈错排公式的推导及应用
    Python爬虫笔记(一):爬虫基本入门
    机器理解大数据秘密:聚类算法深度剖析
    想了解概率图模型?你要先理解图论的基本定义与形式
    MATLAB命令大全+注释小结
    【批处理学习笔记】第二十九课:ASCII码
    【批处理学习笔记】第二十八课:声音和控制
    【批处理学习笔记】第二十七课:视窗
    【批处理学习笔记】第二十六课:返回值
  • 原文地址:https://www.cnblogs.com/tanshaoshenghao/p/10537830.html
Copyright © 2011-2022 走看看