2012java蓝桥杯本科选拔赛

本文通过一系列数学和编程挑战,包括黄金分割在数列中的体现、海盗饮酒问题、汉诺塔的移动次数计算、低碳生活大奖赛的计分规则、寻找字符串中首个数字、割圆法求圆周率、最大值快速查找、处理矩形关系、智力训练中数字组合和泊松分酒问题,展示了数学和编程思维在解决实际问题中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.黄金分割数0.618与美学有重要的关系。舞台上报幕员所站的位置大约就是舞台宽度的0.618处,墙上的画像一般也挂在房间高度的0.618处,甚至股票的波动据说也能找到0.618的影子...

    黄金分割数是个无理数,也就是无法表示为两个整数的比值。0.618只是它的近似值,其真值可以通过对5开方减去1再除以2来获得,我们取它的一个较精确的近似值:0.618034

    有趣的是,一些简单的数列中也会包含这个无理数,这很令数学家震惊!

    1 3 4 7 11 18 29 47 .... 称为“鲁卡斯队列”。它后面的每一个项都是前边两项的和。

    如果观察前后两项的比值,即:1/3,3/4,4/7,7/11,11/18 ... 会发现它越来越接近于黄金分割数!

    你的任务就是计算出从哪一项开始,这个比值四舍五入后已经达到了与0.618034一致的精度。

请写出该比值。格式是:分子/分母。比如:29/47

package LanQiaoBei;

import java.text.DecimalFormat;

public class Java2012_1 {

	public static void main(String[] args) {
		double x = 1;
		double y = 3;
		double x1 = x;
		String finalValue = "0.618034";
		double temp = 1/3;
		String result = "";
		DecimalFormat df = new DecimalFormat("0.000000");
		while(!finalValue.equals(result)) {
			x1 = x;
			x = y;
			y += x1; 
			temp = x/y;
			result = df.format(temp);
		}
		System.out.println((int)x + "/" + (int)y);
	}

}

 

2.有一群海盗(不多于20人),在船上比拼酒量。过程如下:打开一瓶酒,所有在场的人平分喝下,有几个人倒下了。再打开一瓶酒平分,又有倒下的,再次重复...... 直到开了第4瓶酒,坐着的已经所剩无几,海盗船长也在其中。当第4瓶酒平分喝下后,大家都倒下了。

    等船长醒来,发现海盗船搁浅了。他在航海日志中写到:“......昨天,我正好喝了一瓶.......奉劝大家,开船不喝酒,喝酒别开船......

    请你根据这些信息,推断开始有多少人,每一轮喝下来还剩多少人。

    如果有多个可能的答案,请列出所有答案,每个答案占一行。

    格式是:人数,人数,...

    例如,有一种可能是:20,5,4,2,0

package LanQiaoBei;

public class Java2012_2 {

	public static void main(String[] args) {
		sovle(20, 0, new int[4]);
	}

	private static void sovle(int cur, int index, int arr[]) {
		if(index== 4) {
			double temp = 0;
			for(int i=1; i<4; i++) {
				if(arr[i]>=arr[i-1]) {
					return;
				}
			}
			for(int i=0; i<4; i++) {
				temp +=  1.0/arr[i];
			}
			if(temp == 1.0) {
				for(int i=0; i<4; i++) {
					System.out.print(arr[i] + " ");
				}
				System.out.println("0");
			}
		} else {
			for(int i=cur; i>=0; i--) {
				arr[index] = i;
				sovle(cur, index+1, arr);
			}
		}
	}

}


 

3.汉诺塔(又称河内塔)问题是源于印度一个古老传说的益智玩具。

    大梵天创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上按照大小顺序摞着64片黄金圆盘。大梵天命令婆罗门把圆盘从下面开始按大小顺序重新摆放在另一根柱子上(可以借助第三根柱子做缓冲)。并且规定,在小圆盘上不能放大圆盘,在三根柱子之间一次只能移动一个圆盘。

    如图【1.jpg】是现代“山寨”版的该玩具。64个圆盘太多了,所以减为7个,金刚石和黄金都以木头代替了......但道理是相同的。

    据说完成大梵天的命令需要太多的移动次数,以至被认为完成之时就是世界末日!

    你的任务是精确计算出到底需要移动多少次。

    很明显,如果只有2个圆盘,需要移动3次。

    圆盘数为3,则需要移动7次。

那么64个呢?

package LanQiaoBei;

import java.math.BigInteger;

public class TestHanoi {
	
	public static int count = 0;

	public static void main(String[] args) {
//		char A, B, C;
//		A = 'A';
//		B = 'B';
//		C = 'C';
//		for(int i=1; i<10; i++) {
//			count = 0;
//			hanoi(A, B, C, i);//通过写出hanoi找出规律
//			System.out.println(count);
//		}
		int start = 1;
		int end = 64;
		BigInteger temp = BigInteger.valueOf(2);
		BigInteger result = BigInteger.ZERO;
		for(int i=start; i<=end; i++) {
			result = result.multiply(temp).add(BigInteger.ONE);
		}
		System.out.println(result);
	}

//	private static void hanoi(char a, char b, char c, int n) {
//		if(n == 1) {
//			return;
//		} else {
//			hanoi(a, b, c, n-1);//将a塔上n-1盘借助b挪到c塔上
//			move(a, b);//将a塔上剩余的一个盘挪到b上
//			hanoi(c, a, b, n-1);//把c塔上n-1个盘借助a挪到b上
//		}
//	}

//	private static void move(char a, char b) {
//		//System.out.println(a+"-->>"+b);
//		count ++;
//	}
}

4. 某电视台举办了低碳生活大奖赛。题目的计分规则相当奇怪:

    每位选手需要回答10个问题(其编号为110),越后面越有难度。答对的,当前分数翻倍;答错了则扣掉与题号相同的分数(选手必须回答问题,不回答按错误处理)。

    每位选手都有一个起步的分数为10分。

    某获胜选手最终得分刚好是100分,如果不让你看比赛过程,你能推断出他(她)哪个题目答对了,哪个题目答错了吗?

    如果把答对的记为1,答错的记为0,则10个题目的回答情况可以用仅含有10的串来表示。例如:0010110011 就是可能的情况。

    你的任务是算出所有可能情况。每个答案占一行。

package LanQiaoBei;

public class Java2012_4 {

	public static void main(String[] args) {
		sovle(0, new int[10]);
	}

	private static void sovle(int index, int arr[]) {
		if(index == 10) {
			int temp = 10;
			for(int i=0; i<10; i++) {
				temp = arr[i]==1 ? temp<<1 : temp-i-1;
			}
			if(temp == 100) {
				for(int i=0; i<10; i++) {
					System.out.print(arr[i]);
				}
				System.out.println();
			}
		} else {
				for(int i=0; i<=1; i++) {
					arr[index] = i;
					sovle(index+1, arr);
				}
				
		}
	}

}


5.以下的静态方法实现了:把串s中第一个出现的数字的值返回。

如果找不到数字,返回-1

例如:

s = "abc24us43"  则返回2

s = "82445adb5"  则返回8

s = "ab"   则返回-1   

package LanQiaoBei;

public class Java2012_5 {

	public static void main(String[] args) {
		int i = getFirstNum("sdfsfssfsdf");
		System.out.println(i);
	}

	public static int getFirstNum(String s) {
		if(s==null || s.length()==0) return -1;
		char ch = s.charAt(0);
		if(ch>='0' && ch<='9') return ch-'0';//111111111
		return getFirstNum(s.substring(1, s.length()));//222222222
	}
}


6.南北朝时,我国数学家祖冲之首先把圆周率值计算到小数点后六位,比欧洲早了1100年!他采用的是称为“割圆法”的算法,实际上已经蕴含着现代微积分的思想。

    如图【1.jpg】所示,圆的内接正六边形周长与圆的周长近似。多边形的边越多,接近的越好!我们从正六边形开始割圆吧。

    如图【2.jpg】所示,从圆心做弦的垂线,可把6边形分割为12边形。该12边形的边长a'的计算方法很容易利用勾股定理给出。之后,再分割为正24边形,....如此循环会越来越接近圆周。

    之所以从正六边开始,是因为此时边长与半径相等,便于计算。取半径值为1,开始割圆吧!

    以下代码描述了割圆过程。

    

    程序先输出了标准圆周率值,紧接着输出了不断分割过程中多边形边数和所对应的圆周率逼近值。

package LanQiaoBei;

public class Java2012_6 { 
	public static void main(String[] args){ 
		System.out.println("标准 " + Math.PI); 
		double a = 1;  
		int n = 6; 
		for(int i=0; i<10; i++) 
		{ 
			double b = Math.sqrt(1-(a/2)*(a/2)); 
			a = Math.sqrt((1-b)*(1-b) + (a/2)*(a/2)); 
			n = n << 1; //填空 
			System.out.println(n + "  " + n*a*Math.sqrt(1-a/2*a/2)/2);  // 填空 
		}  
	} 

} 


7.[12,127,85,66,27,34,15,344,156,344,29,47,....]  

    这是某设备测量到的工程数据。

    因工程要求,需要找出最大的5个值。

    一般的想法是对它排序,输出前5个。但当数据较多时,这样做很浪费时间。因为对输出数据以外的数据进行排序并非工程要求,即便是要输出的5个数字,也并不要求按大小顺序,只要找到5个就可以。

    以下的代码采用了另外的思路。考虑如果手里已经抓着5个最大数,再来一个数据怎么办呢?让它和手里的数据比,如果比哪个大,就抢占它的座位,让那个被挤出来的再自己找位子,....

package LanQiaoBei;

import java.util.Arrays;
import java.util.List;
import java.util.Vector;

public class Java2012_7 
{ 
	public static void main(String[] args)  { 
		List<Integer> lst = new Vector<Integer>(); 
		lst.addAll(Arrays.asList(12,127,85,66,27,34,15,344,156,344,29,47));   
		System.out.println(max5(lst)); 
	} 

	public static List<Integer> max5(List<Integer> lst){ 
		if(lst.size()<=5) return lst; 
		int a = lst.remove(0);  // 填空 
		List<Integer> b = max5(lst); 
		for(int i=0; i<b.size(); i++){ 
			int t = b.get(i); 
			if(a>t){ 
				b.set(i, a);  // 填空 
				a = t;   
			} 
		} 
		return b; 
	} 
} 


8.在编写图形界面软件的时候,经常会遇到处理两个矩形的关系。

    如图【1.jpg】所示,矩形的交集指的是:两个矩形重叠区的矩形,当然也可能不存在(参看【2.jpg】)。两个矩形的并集指的是:能包含这两个矩形的最小矩形,它一定是存在的。

    本题目的要求就是:由用户输入两个矩形的坐标,程序输出它们的交集和并集矩形。

    矩形坐标的输入格式是输入两个对角点坐标,注意,不保证是哪个对角,也不保证顺序(你可以体会一下,在桌面上拖动鼠标拉矩形,4个方向都可以的)。

    输入数据格式:

x1,y1,x2,y2

x1,y1,x2,y2  

    数据共两行,每行表示一个矩形。每行是两个点的坐标。x坐标在左,y坐标在右。坐标系统是:屏幕左上角为(0,0)x坐标水平向右增大;y坐标垂直向下增大。

    要求程序输出格式:

x1,y1,长度,高度

x1,y1,长度,高度

    也是两行数据,分别表示交集和并集。如果交集不存在,则输出“不存在”

    前边两项是左上角的坐标。后边是矩形的长度和高度。

    例如,用户输入:

100,220,300,100

150,150,300,300

    则程序输出:

150,150,150,70

100,100,200,200

    例如,用户输入:

10,10,20,20

30,30,40,40

    则程序输出:

不存在

10,10,30,30

package LanQiaoBei;

import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.util.Scanner;

/**
 * 100,220,300,100
 * 150,150,300,300
 * 10,10,20,20
 * 30,30,40,40
 * @author wangfengyang
 *
 */
public class Java2012_8 
{ 
	public static void main(String[] args)  { 
		Scanner sc = new Scanner(System.in);
		int x1, y1, x2, y2;
		String str = "";
		Rectangle rectranglea, rectrangleb;
		while(sc.hasNext()) {
			str = sc.next();
			String[] strArr = str.split(",");
			x1 = Integer.parseInt(strArr[0]);
			y1 = Integer.parseInt(strArr[1]);
			x2 = Integer.parseInt(strArr[2]);
			y2 = Integer.parseInt(strArr[3]);
			rectranglea = getRectrangle(x1, y1, x2, y2);
			str = sc.next();
			strArr = str.split(",");
			x1 = Integer.parseInt(strArr[0]);
			y1 = Integer.parseInt(strArr[1]);
			x2 = Integer.parseInt(strArr[2]);
			y2 = Integer.parseInt(strArr[3]);
			rectrangleb = getRectrangle(x1, y1, x2, y2);

			Rectangle2D intersectionRect = intersection(rectranglea, rectrangleb);
			if(intersectionRect == null) {
				System.out.println("不存在");
			} else {
				System.out.printf("%.0f,%.0f,%.0f,%.0f\n", intersectionRect.getX(), intersectionRect.getY(), intersectionRect.getWidth(), intersectionRect.getHeight());
			}
			
			Rectangle2D unionRect = union(rectranglea, rectrangleb);
			System.out.printf("%.0f,%.0f,%.0f,%.0f\n", unionRect.getX(), unionRect.getY(), unionRect.getWidth(), unionRect.getHeight());
		
		}
		
		
	}
	
	

	private static Rectangle2D intersection(Rectangle rectranglea,
			Rectangle rectrangleb) {
		Rectangle2D temp = rectranglea.createIntersection(rectrangleb);
		if(temp.getHeight()<0 || temp.getWidth()<0) {
			return null;
		} else {
			return temp;
		}
	}

	private static Rectangle2D union(Rectangle rectranglea, Rectangle rectrangleb) {
		return rectranglea.createUnion(rectrangleb);
	}

	private static Rectangle getRectrangle(int x1, int y1, int x2, int y2) {
		return new Rectangle(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2));
	} 

} 


9.匪警请拨110,即使手机欠费也可拨通!

    为了保障社会秩序,保护人民群众生命财产安全,警察叔叔需要与罪犯斗智斗勇,因而需要经常性地进行体力训练和智力训练!

    某批警察叔叔正在进行智力训练:

    1 2 3 4 5 6 7 8 9 = 110;

    请看上边的算式,为了使等式成立,需要在数字间填入加号或者减号(可以不填,但不能填入其它符号)。之间没有填入符号的数字组合成一个数,例如:12+34+56+7-8+9 就是一种合格的填法;123+4+5+67-89 是另一个可能的答案。

    请你利用计算机的优势,帮助警察叔叔快速找到所有答案。

    每个答案占一行。形如:

12+34+56+7-8+9

123+4+5+67-89

......

    已知的两个答案可以输出,但不计分。   

    各个答案的前后顺序不重要。

package LanQiaoBei;

public class Java2012_9 
{ 
	public static void main(String[] args)  { 
		sovle(0, new char[8]);
	} 

	public static void sovle(int cur, char[] arr){ 
		if(cur == 8) {
			String str = "1";
			String strTemp = "1";
			for(int i=0; i<8; i++) {
				if(arr[i] == 'a') {
					str += (i+2);
					strTemp += (i+2);
				} else if(arr[i] == 'b') {
					str += "x";
					str += (i+2);
					strTemp += "+";
					strTemp += (i+2);
				} else {
					str += "x";
					str += (i+2);
					strTemp += "-";
					strTemp += (i+2);
				}
			}
			//System.out.println(str);
			String strArr[] = str.split("x");
			int result = Integer.parseInt(strArr[0]);
			int j=0;
			for(int i=1; i<strArr.length; i++) {
				while(true) {
					if(arr[j] == 'b') {
						result += Integer.parseInt(strArr[i]);
						j++;
						break;
					} else if(arr[j]=='c') {
						result -= Integer.parseInt(strArr[i]);
						j++;
						break;
					} else {
						j++;
					}
				}
			}
			if(result == 110) {
				System.out.println(strTemp);
			}
		} else {
			for(int i=0; i<3; i++) {
				arr[cur] = (char)(i+'a');
				sovle(cur+1, arr);
			}
		}
	} 
} 


10.泊松是法国数学家、物理学家和力学家。他一生致力科学事业,成果颇多。有许多著名的公式定理以他的名字命名,比如概率论中著名的泊松分布。

    有一次闲暇时,他提出过一个有趣的问题,后称为:“泊松分酒”。在我国古代也提出过类似问题,遗憾的是没有进行彻底探索,其中流传较多是:“韩信走马分油”问题。

    有3个容器,容量分别为12升,8升,5升。其中12升中装满油,另外两个空着。要求你只用3个容器操作,最后使得某个容器中正好有6升油。

    下面的列表是可能的操作状态记录:

12,0,0

4,8,0

4,3,5

9,3,0

9,0,3

1,8,3

1,6,5

    每行3个数据,分别表示1286升容器中的油量

    第一行表示初始状态,第二行表示把12升倒入8升容器后的状态,第三行是8升倒入5升,...

    当然,同一个题目可能有多种不同的正确操作步骤。

    本题目的要求是,请你编写程序,由用户输入:各个容器的容量,开始的状态,和要求的目标油量,程序则通过计算输出一种实现的步骤(不需要找到所有可能的方法)。如果没有可能实现,则输出:“不可能”。

    例如,用户输入:

12,8,5,12,0,0,6

    用户输入的前三个数是容器容量(由大到小),接下来三个数是三个容器开始时的油量配置,最后一个数是要求得到的油量(放在哪个容器里得到都可以)

    则程序可以输出(答案不唯一,只验证操作可行性):

12,0,0

4,8,0

4,3,5

9,3,0

9,0,3

1,8,3

1,6,5

    每一行表示一个操作过程中的油量状态。

package LanQiaoBei;

import java.util.ArrayDeque;
import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import java.util.Scanner;

/**
 * 12 8 5 12 0 0 6
 * 12 8 5 3 0 0 6
 * @author Administrator
 *
 */
public class Java2012_10_2 {
	
	private static Queue<State> queue = new ArrayDeque<State>();
	private static Map<State, Integer> map = new HashMap<State, Integer>();

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int v1, v2, v3, currentV1, currentV2, currentV3, result;
		v1 = sc.nextInt();
		v2 = sc.nextInt();
		v3 = sc.nextInt();
		currentV1 = sc.nextInt();
		currentV2 = sc.nextInt();
		currentV3 = sc.nextInt();
		result = sc.nextInt();
		
		State stateStart = new Java2012_10_2().new State(currentV1, currentV2, currentV3);
		
		State stateResult = solve(v1,v2,v3, result, stateStart);
		
		if(stateResult == null) {
			System.out.println("不可能");
		} else {
			showResult(stateResult);
		}
		
		
		
	}
	
	private static void showResult(State stateResult) {
		if(stateResult != null) {
			showResult(stateResult.parent);
			System.out.println(stateResult.v1 + "," + stateResult.v2 + "," + stateResult.v3);
		}
	}

	private static State solve(int v1, int v2, int v3, int result, State stateStart) {
		queue.add(stateStart);
		map.put(stateStart, null);
		State tempState = null;
		while(!queue.isEmpty()) {
			tempState = queue.poll();
			if(tempState.isResult(tempState, result)) return tempState;
			tempState.daoYou(v1, v2, v3, result);
			System.out.println(map.size());
		}
		return null;
	}

	class State{
		int v1;
		int v2;
		int v3;
		State parent = null;
		
		public State(int v1, int v2, int v3) {
			this.v1 = v1;
			this.v2 = v2;
			this.v3 = v3;
		}
		
		public void daoYou(int c1, int c2, int c3,int result) {
			subDaoYou1(c1, c2, c3, result);
			subDaoYou2(c1, c2, c3, result);
			subDaoYou3(c1, c2, c3, result);
			subDaoYou4(c1, c2, c3, result);
			subDaoYou5(c1, c2, c3, result);
			subDaoYou6(c1, c2, c3, result);
		}
		
		private void subDaoYou1(int c1, int c2, int c3, int result) {
			if(v1 != 0) {
				if(v2 != c2) {
					State temp = null;
					if(v1 > c2-v2) {
						temp = new State(v1-c2+v2, c2, v3);
					} else {
						temp = new State(0, v2+v1, v3);
					}
					if(!map.containsKey(temp)) {
						temp.parent = this;
						map.put(temp, null);
						queue.add(temp);
					}
				}
			}
		}
		
		private void subDaoYou2(int c1, int c2, int c3, int result) {
			if(v1 != 0) {
				if(v3 != c3) {
					State temp = null;
					if(v1 > c3-v3) {
						temp = new State(v1-c3+v3, v2, c3);
					} else {
						temp = new State(0, v2, v3+v1);
					}
					if(!map.containsKey(temp)) {
						temp.parent = this;
						map.put(temp, null);
						queue.add(temp);
					}
				}
			}
		}
		
		private void subDaoYou3(int c1, int c2, int c3, int result) {
			if(v2 != 0) {
				if(v1 != c1) {
					State temp = null;
					if(v2 > c1-v1) {
						temp = new State(c1, v2+v1-c1, v3);
					} else {
						temp = new State(v1+v2, 0, v3);
					}
					if(!map.containsKey(temp)) {
						temp.parent = this;
						map.put(temp, null);
						queue.add(temp);
					}
				}
			}
		}
		
		private void subDaoYou4(int c1, int c2, int c3, int result) {
			if(v2 != 0) {
				if(v3 != c3) {
					State temp = null;
					if(v2 > c3-v3) {
						temp = new State(v1, v2+v3-c3, c3);
					} else {
						temp = new State(v1, 0, v2+v3);
					}
					if(!map.containsKey(temp)) {
						temp.parent = this;
						map.put(temp, null);
						queue.add(temp);
					}
				}
			}
		}
		
		private void subDaoYou5(int c1, int c2, int c3, int result) {
			if(v3 != 0) {
				if(v1 != c1) {
					State temp = null;
					if(v3 > c1-v1) {
						temp = new State(c1, v2, v1+v3-c1);
					} else {
						temp = new State(v1+v3, v2, 0);
					}
					if(!map.containsKey(temp)) {
						temp.parent = this;
						map.put(temp, null);
						queue.add(temp);
					}
				}
			}
		}
		
		private void subDaoYou6(int c1, int c2, int c3, int result) {
			if(v3 != 0) {
				if(v2 != c2) {
					State temp = null;
					if(v3 > c2-v2) {
						temp = new State(v1, c2, v3+v2-c2);
					} else {
						temp = new State(v1, v2+v3, 0);
					}
					if(!map.containsKey(temp)) {
						temp.parent = this;
						map.put(temp, null);
						queue.add(temp);
					}
				}
			}
		}

		public boolean equals(Object arg0) {
			State temp = (State)arg0;
			return this.v1==temp.v1 && this.v2==temp.v2 && this.v3==temp.v3;
		}
		
		@Override
		public int hashCode() {
			return (this.v1+""+this.v2+""+this.v3).hashCode();
		}
		
		public boolean isResult(State state, int result) {
			return state.v1==result || state.v2==result || state.v3==result;
		}
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值