2012 3rd Blue Bridge Cup Software Competition Java Language Undergraduate Group Preliminary Exam Questions

2012 3rd Blue Bridge Cup Software Competition Java Language Undergraduate Group Preliminary Exam Questions

(Note: 1-4 questions are filled in the blanks for results, 5-7 are for programs, and 8-10 are programming questions)

 

1. The golden ratio is 0.618

    The golden ratio of 0.618 has an important relationship with aesthetics. The position where the announcer stands on the stage is about 0.618 of the width of the stage, and the portraits on the wall are generally hung at 0.618 of the height of the room. Even the fluctuation of the stock market is said to be able to find the shadow of 0.618...

    The golden ratio is an irrational number, that is, it cannot be expressed as the ratio of two integers. 0.618 is just an approximation of it, its true value can be obtained by subtracting 1 from the root of 5 and dividing by 2, we take a more accurate approximation of it: 0.618034

    Interestingly, this irrational number is also included in some simple series, which is shocking to mathematicians!

    1 34 7 11 18 29 47 .... is called the "Lucas Queue". Each term after it is the sum of the previous two.

    If you observe the ratio of the two items before and after, namely: 1/3, 3/4, 4/7, 7/11, 11/18... You will find that it is getting closer and closer to the golden ratio!

    Your task is to figure out which one to start with, and this ratio has been rounded to an accuracy consistent with 0.618034.

    Please write the ratio. The format is: numerator/denominator. For example: 29/47

	public static void main(String[] args)
	{
		double x = 1;
		double y = 3;
		double res = x / y * 1000000;
		while (618034 != new BigDecimal(String.valueOf(res)).setScale(0, BigDecimal.ROUND_HALF_UP).intValue())//调用BigDecimal四舍五入
		{ 
			double temp = x + y;
			x = y;
			y = temp;
			res = x / y * 1000000;
		}
		System.out.println((int) x + "/" + (int) y);
	}


 

2. Pirates _

    A group of pirates (no more than 20 people) competed on the boat for drinking. The process is as follows: a bottle of wine is opened, all the people present drink it equally, and a few people fall. One more bottle of wine is opened and it is divided equally, and another one falls down, repeating again...until the fourth bottle of wine is opened, there are not many people sitting, and the pirate captain is among them. When the fourth bottle of wine was evenly drank, everyone fell.

    When the captain woke up, he found that the pirate ship had run aground. He wrote in the logbook: "...Yesterday, I happened to drink a bottle.... I advise everyone, don't drink when sailing, and don't sail when drinking..."

    Based on this information, please deduce how many people there are at the beginning and how many people are left after drinking each round.

    If there are multiple possible answers, please list all the answers on one line each.

    The format is: number, number, ...

    For example, one possibility is: 20,5,4,2,0

	public static void main(String[] args)
	{
		double n = 1;
		for(int i = 4;i<=20;i++)//total 
		{
			for(int j = 4;j<=i;j++)
			{
				for(int k = 3;k<=j;k++)
				{
					for(int x = 2;x<=k;x++ )
					{
						if(n/i+n/j+n/k+n/x == 1)
						{
							System.out.println(i+","+j+","+k+","+x+",0");
						}
					}
				}
			}
		}
	}


 

3. Tower of Hanoi

    The Tower of Hanoi (also known as the Tower of Hanoi) is an educational toy that originated from an ancient Indian legend.

    When Brahma created the world, he made three diamond pillars, on which 64 golden discs were stacked in order from bottom to top. Brahma ordered the Brahman to rearrange the disc on another pillar in order of size from the bottom (the third pillar can be used as a buffer). And it is stipulated that the disc cannot be enlarged on the small disc, and only one disc can be moved at a time between the three columns.

    Figure [1.jpg] is a modern "cottage" version of the toy. 64 discs was too much, so it was reduced to 7, and both diamonds and gold were replaced by wood...but the logic is the same.

    It is said that completing Brahma's orders requires so many moves that it is considered the end of the world when done!

    Your task is to figure out exactly how many moves are required.

    Obviously, if there are only 2 discs, 3 moves are required.

    If the number of discs is 3, you need to move 7 times.

    What about 64?


【1.jpg】

public static void main(String[] args)
{	
	System.out.println(BigInteger.valueOf(2).pow(64).subtract(BigInteger.valueOf(1)).toString());
}


4. Quiz competition

    A TV station held a low-carbon life competition. The scoring rules for the questions are rather strange:

    Each player needs to answer 10 questions (numbered from 1 to 10), the more difficult the later. If the answer is correct, the current score will be doubled; if the answer is wrong, the same score as the question number will be deducted (the contestant must answer the question, and if the answer is not answered, it will be treated as an error).

    Each competitor has a starting score of 10.

    The final score of a winning player is exactly 100 points. If you are not allowed to watch the game, can you infer which question he (she) answered correctly and which question is wrong?

    If the correct answer is recorded as 1, and the wrong answer is recorded as 0, the answers to the 10 questions can be represented by a string containing only 1 and 0. For example: 0010110011 is a possible case.

    (hint enumeration: 2^10=1024)

    Your task is to work out all possible scenarios. One line for each answer.

    public static void main(String[] args)
    {
       func(2,0,9,"0");
       func(2,1,20,"1");
    }
   
    static void func(int n,int judge,int sum ,String res)
    {
       if(n==11)
       {
           if(sum == 100)
           {
              System.out.println(res);
           }
           return;
       }
       func(n+1,0,sum-n,res+"0");
       func(n+1,1,sum*2,res+"1");
    }


5. Return the value of the first occurrence of the number in the string s

The following static method is implemented: Returns the value of the first occurrence of the number in the string s.

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

例如:

s = "abc24us43"  则返回2

s = "82445adb5"  则返回8

s = "ab"   则返回-1  

请分析代码逻辑,并推测划线处的代码。

public static int getFirstNum(String s)
{
   if(s==null|| s.length()==0) return -1;
   charc = s.charAt(0);
   if(c>='0'&& c<='9') return _____________; //填空
   return___________________________; //填空
}

答案:c-'0'     getFirstNum(s.substring(1))


 

6.PI(分割圆)

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

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

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

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

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

    程序先输出了标准圆周率值,紧接着输出了不断分割过程中多边形边数和所对应的圆周率逼近值。
    请分析代码逻辑,并推测划线处的代码。

                                                     

【1.jgp】                                                                        【2.jpg】

public class B21
{
	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 = ______________; //填空
			System.out.println(n + "  " + _______________);  // 填空
		}
	}
}

答案:2*n           a*n/2    

 

 

7.Max5最大5个数

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

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

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

    The general idea is to sort it and output the top 5. But when there is a lot of data, doing so is a waste of time. Because sorting data other than the output data is not an engineering requirement, even if the 5 numbers to be output are not required to be in order of size, as long as 5 numbers are found.

    The code below takes a different approach. Consider if you already have the 5 largest numbers in your hand, what should you do with another data? Compare it with the data in your hand, if it is bigger than whichever one is bigger, grab its seat, and let the one who is squeezed out find a seat by himself. …

    Please analyze the code logic and speculate on the code at the line.

import java.util.*;
public class B23
{
   publicstatic List<Integer> max5(List<Integer> lst)
   {
     if(lst.size()<=5)return lst;
     int a = __________________________________ // 填空
     List<Integer>b = max5(lst);
     for(inti=0; i<b.size(); i++)
     {
        intt = b.get(i);
        if(a>t)
        {
          ________________________;  // 填空
          a= t; 
        }
     }
     returnb;
   }
   publicstatic 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));
   }
}

答案: (lst=lst.subList(1, lst.size())).get(0);             b.set(i, a);



8. Rectangle intersection

    When writing graphical interface software, it is often encountered to deal with the relationship between two rectangles.

    As shown in Figure [1.jpg], the intersection of rectangles refers to the rectangle in the overlapping area of ​​two rectangles, which of course may not exist (see [2.jpg]). The union of two rectangles refers to the smallest rectangle that can contain these two rectangles, and it must exist.

    The requirements of this topic are: the user inputs the coordinates of two rectangles, and the program outputs their intersection and union rectangles.

 

    The input format of the rectangle coordinates is to input the coordinates of two opposite corners. Note that it is not guaranteed which corners are opposite, nor the order (you can experience it by dragging the mouse on the desktop to pull the rectangle, all four directions are available).

 

    Input data format:

    x1,y1,x2,y2

    x1,y1,x2,y2

    There are two rows of data, each row represents a rectangle. Each row is the coordinates of two points. The x coordinate is on the left and the y coordinate is on the right. The coordinate system is: the upper left corner of the screen is (0,0), the x coordinate increases horizontally to the right; the y coordinate increases vertically downward.

    Required program output format:

    x1,y1,length,height

    x1,y1,length,height

    It is also two rows of data, representing intersection and union, respectively. If the intersection does not exist, output "does not exist"

The first two items are the coordinates of the upper left corner. The back is the length and height of the rectangle.

 

For example, the user enters:

100,220,300,100

150,150,300,300

Then the program outputs:

150,150,150,70

100,100,200,200

For example, the user enters:

10,10,20,20

30,30,40,40

Then the program outputs:

does not exist

10,10,30,30

Picture1.jpg

Picture 2.jpg

       

【图1.jpg】                                                                                       【图2.jpg】                                   

	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);

		Rectangle r1 = null;
		Rectangle r2 = null;
		while (true)
		{
			r1 = getMyRectangle(sc.nextLine());
			r2 = getMyRectangle(sc.nextLine());
			
			Rectangle intersect = r1.intersection(r2);
			if (intersect.width<=0)
			{
				System.out.println("不存在交集");
			} else
			{
				System.out.println(intersect.x + "," + intersect.y + ","
						+ intersect.width + ","
						+ intersect.height);
			}

			Rectangle union = r1.union(r2);

			System.out.println(union.x + "," + union.y + ","
					+ union.width + "," + union.height);

		}
	}
	
	public static Rectangle getMyRectangle(String s)
	{
		String[] ps = s.split(",");
		int []t = new int[4];
		for(int i=0;i<4;i++)
		{
			t[i] = Integer.parseInt(ps[i].replaceAll(" ", ""));
		}
		if(t[0]>t[2])
		{
			int temp = t[0];
			t[0]=t[2];
			t[2] =temp;
		}
		if(t[1]>t[3])
		{
			int temp = t[1];
			t[1]=t[3];
			t[3] =temp;
		}
		
		return new Rectangle(t[0],t[1],t[2]-t[0],t[3]-t[1]);
	}


 

9.1~9加减得到110

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

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

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

    1 23 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

    ......

    已知的两个答案可以输出,但不计分。
    各个答案的前后顺序不重要。

	public static void main(String[] args)
	{
		func(8, "");
	}

	public static void func(int n, String ops)
	{
		if (n == 0)
		{
			String res = "1";
			for (int i = 0; i < ops.length(); i++)
			{
				res += ops.charAt(i) + String.valueOf(i + 2);
			}
			res = res.replace(" ", "");
			ops = ops.replaceAll(" ", "");
			int []num = new int[9];
			int temp = 0;
			int nlen = 0;
			for(int i=0;i<res.length();i++)
			{
				if(res.charAt(i) == '+'||res.charAt(i) == '-')
				{
					num[nlen] = Integer.parseInt( res.substring(temp,i));
					temp = i+1;
					nlen++;
				}
			}
			num[nlen] = Integer.parseInt( res.substring(temp));
			int sum = num[0];
			for (int i = 0; i < ops.length(); i++)
			{
				if (ops.charAt(i) == '+')
				{
					sum += num[i+1];
				} else
				{
					sum -= num[i+1];
				}
			}
			if(sum==110)
			{
				System.out.println(res);
			}
			return;
		}
		func(n - 1, ops + " ");
		func(n - 1, ops + "+");
		func(n - 1, ops + "-");
	}


结果:

123+4+5+67-89

123+4-5-6-7-8+9

123-4+5-6-7+8-9

123-4-5+6+7-8-9

12+34+56+7-8+9

12+3+45+67-8-9

12-3+4-5+6+7+89

1+234-56-78+9

1+2+34+5+67-8+9

1-2+3+45-6+78-9

 


 

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个数据,分别表示12,8,6升容器中的油量

    第一行表示初始状态,第二行表示把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

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

static int a, b, c;
public static void main(String[] args)
{
	Scanner sc = new Scanner(System.in);
	String s = sc.nextLine();
	String[] sn = s.split(",");
	a = Integer.parseInt(sn[0]);
	b = Integer.parseInt(sn[1]);
	c = Integer.parseInt(sn[2]);
	int a1 = Integer.parseInt(sn[3]);
	int b1 = Integer.parseInt(sn[4]);
	int c1 = Integer.parseInt(sn[5]);
	int res = Integer.parseInt(sn[6]);
	if(a1+b1+c1>=res)
	{
		pour(a1, b1, c1, res);
	}
	else
	{
		System.out.println("找不到解决方案");
	}
}

static String allKind = "";
public static void pour(int a1, int b1, int c1, int res)
{
	String kind = a1 + "," + b1 + "," + c1;
	System.out.println(kind);
	if(allKind.contains(kind))
	{
		System.out.println("找不到解决方案");
		return;
	}
	allKind+="["+kind+"]";
	if (a1 == res || b1 == res || c1 == res)
	{
		return;
	}
	if (c1 == 0 && b1 != 0)
	{
		pour(a1, b1 > c ? b1 - c : 0, b1 > c ? c : b1, res);
	}
	else
	{
		if (c1 == c)
		{
			pour(a-a1>c?a1+c:a,b1,a-a1>c?0:a1+c-a,res);
		}
		else
		{
			if (a1!=0&&b1 != b)
			{
				pour(a1 > b-b1 ? a1 - b +b1: 0, a1 > b-b1 ? b : b1+a1, c1,res);
			}
			else
			{
				if(b1!=0&&c1!=c)
				pour(a1, b1 > c-c1 ? b1 - c+c1 : 0, b1 > c-c1 ? c : b1+c1,res);
				else
					System.out.println("找不到解决方案");
			}
		}
	}
}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325560223&siteId=291194637