2013 Blue Bridge Cup Java group B preliminaries

1. The week at the end of the century

A cult once called December 31, 1999 the end of the world. Of course, the rumor has been broken.

Others say that December 31, the end of a century in the future, will be on Monday....

Interestingly, December 31 of any year at the end of the century cannot be Monday!! 

Therefore, the "rumor manufacturer" was revised to Sunday......

1999 December 31, 2008 is Friday. May I ask: which year is the end of the century nearest to us in the future (i.e xx99 December 31 of (year) happens to be Sunday (i.e. Sunday)?

Please answer the year (only write this 4-digit integer, do not write redundant information such as December 31)

Answer: 2299
getDay() function is to get the week corresponding to the date, and 0 represents Sunday

code

public static void main(String[] args) {
		for(int i=2000;i<2999;i++) {
			String d2 = i+"/12/31";
			Date data2 = new Date(d2);
			if(data2.getDay()==0 && String.valueOf(i).contains("99")) {
				System.out.println(i);
			}
		}
	}

2. Careless formula

Xiao Ming is an acute child. When he was in primary school, he often copied the questions written by the teacher on the blackboard wrong.

Once, the teacher's question was: 36 x 495 = ?

He copied it: 396 x 45 = ?

But the result is very dramatic. His answer is actually right!!

Because 36 * 495 = 396 * 45 = 17820

There may be many such coincidences, such as 27 * 594 = 297 * 54

hypothesis a b c d e Representative 1~9 5 different numbers (note that they are different numbers and do not contain 0)

It can meet the following requirements: ab * cde = adb * ce How many kinds of such expressions are there?

Please use the advantages of computers to find all the possibilities and answer the types of different formulas.

The formulas satisfying the commutative law of multiplication are of different kinds, so the answer must be an even number.

The answer is submitted directly through the browser.
Note: only one number indicating the number of final statistical categories shall be submitted, and the solution process or other redundant contents shall not be submitted.
code

public class _02 Sloppy formula {
    public static void main(String[] args) {
        int count = 0;
        for (int a = 1; a < 10; a++) {
            for (int b = 1; b < 10; b++) {
                if (b != a) for (int c = 1; c < 10; c++) {
                    if (c != a && c != b) for (int d = 1; d < 10; d++) {
                        if (d != a && d != b && d!= c) for (int e = 1; e < 10; e++) {
                            if (e!=a && e!=b && e!=c && e!=d)
                                if ((a*10 + b) * (c*100 + d*10 + e) == (a*100 + d*10 + b) * (c*10 + e))
                                    count++;
                        }
                    }

                }
            }
        }
        System.out.println(count);
    }
}

Answer: 142

3. Revitalizing China

Xiao Ming took part in the interesting sports meeting of the school. One of the items is: grid jumping.

There are some squares on the ground. In each square, write a word as follows: (also see p1.jpg)

Start with me
I started to revitalize
Start in revitalization
To revitalize China

During the competition, first stand in the grid with the word "from" in the upper left corner. You can jump horizontally or vertically to the adjacent grid, but you can't jump to the diagonal grid or other positions. Always jump to the end of the word "Hua".


The route required to be skipped just constitutes the sentence "start with me and revitalize China".

Please help Xiao Ming calculate how many possible jumping routes he has?

The answer is an integer. Please submit the number directly through the browser.
Note: do not submit the solution process or other auxiliary description.
code

public static int zh() {
	//dp[i][j] indicates the number of paths to point (i,j)
	int[][] dp = new int[4][5];
	for (int i = 0; i < 4; i++)
		dp[i][0] = 1;
	for (int i = 0; i < 5; i++)
		dp[0][i] = 1;
	for (int i = 1; i < 4; i++) {
		for (int j = 1; j < 5; j++) {
			dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
		}
	}
	return dp[3][4];
}

Answer: 35

4. Gold continued fraction

Golden section number 0.61803... It is an irrational number. This constant is very important and will appear in many engineering problems. Sometimes it needs to be obtained very accurately.

For some precision engineering, the accuracy of constants is very important. Perhaps you have heard of the Hubble Space Telescope. After its first launch, it found a manual processing error. For such a behemoth, in fact, it was only an error in mirror processing that was many times thinner than hair, but it became "myopia"!!


To get back to business, how do we get the golden section number as accurate as possible? There are many ways.

The simpler one is to use continued fraction:

              1
 Gold number = ---------------------
                    1
         1 + -----------------
                      1
             1 + -------------
                        1
                 1 + ---------
                      1 + ...

                       

The more "layers" calculated by this continued fraction, the closer its value is to the golden section.

Please make use of this feature to find a sufficiently accurate value of the golden section number, which is required to be rounded to 100 decimal places.

The value of 3 digits after the decimal point is: 0.618
 The value of 4 digits after the decimal point is: 0.6180
 The value of 5 digits after the decimal point is: 0.61803
 The value 7 digits after the decimal point is: 0.6180340

(note the 0 in the tail, which cannot be ignored)

Your task is to write the golden section value accurate to 100 decimal places.

Note: the mantissa is rounded off! If the mantissa is 0, it should be retained!

Obviously, the answer is a decimal with 100 digits after the decimal point. Please submit the number directly through the browser.
Note: do not submit the solution process or other auxiliary description.

	public static void main(String[] args) {
		BigInteger a = BigInteger.ONE;
		BigInteger b = BigInteger.ONE;
		//Fibonacci's iterative form
		for (int i = 3; i < 500; i++) { //200 300 400 500
			BigInteger t = b;
			b = a.add(b);  //Large integer addition
			a = t;
		}
		//new BigDecimal(a, 100) converts an integer to BigDecimal, specifies the division of a large floating-point number with an accuracy of 100 decimal places, and ROUND_HALF_UP is rounded
		BigDecimal divide = new BigDecimal(a, 100).divide(new BigDecimal(b, 100), BigDecimal.ROUND_HALF_UP);
		
		System.out.println(divide.toPlainString().substring(0, 102));  //[0, 102] 103 character string segmentation
		//ROUND_HALF_DOWN: round; toPlainString() is similar to toString()
	}

Answer: 0.618039887498948204586834365638117720309179805762862135486227052604628189024497072072041893911375

5. Rational number class

A rational number is a number that can be expressed as the ratio of two integers. Generally, we use approximate decimals. However, sometimes, errors are not allowed, and two integers must be used to represent a rational number.

At this time, we can establish a "rational number class". The following code preliminarily realizes this goal. For simplicity, it only provides addition and multiplication.
class Rational
{
	private long ra;
	private long rb;
	
	private long gcd(long a, long b){
		if(b==0) return a;
		return gcd(b,a%b);
	}
	public Rational(long a, long b){
		ra = a;
		rb = b;	
		long k = gcd(ra,rb);
		if(k>1){ //Need to cut points
			ra /= k;  
			rb /= k;
		}
	}
	// addition
	public Rational add(Rational x){
		return ________________________________________;  //Fill in the blanks
	}
	// multiplication
	public Rational mul(Rational x){
		return new Rational(ra*x.ra, rb*x.rb);
	}
	public String toString(){
		if(rb==1) return "" + ra;
		return ra + "/" + rb;
	}
}

Examples of using this class:
Rational a = new Rational(1,3);
Rational b = new Rational(1,6);
Rational c = a.add(b);
System.out.println(a + "+" + b + "=" + c);

Please analyze the code logic, infer the code at the underline, and submit it through the web page
Note: only take the missing code as the answer, and never fill in the redundant code, symbol or explanatory text!!

Answer: new Rational(ra*x.rb+x.ra*rb, rb*x.rb)

6. Three part ranking

There are many classical algorithms for general sorting, such as quick sorting, Hill sorting and so on.

But in practical application, there are often some special requirements more or less. We don't need to apply those classical algorithms, we can establish a better solution according to the actual situation.

For example, sort the numbers in an integer array:

Make negative numbers close to the left, positive numbers close to the right, and 0 in the middle. Note that the characteristics of the problem are: order is not required in negative and positive areas. This feature can be used to end the battle through one linear scan!!

The following procedure achieves this goal.

static void sort(int[] x)
{
	int p = 0;
	int left = 0;
	int right = x.length-1;
	
	while(p<=right){
		if(x[p]<0){
			int t = x[left];
			x[left] = x[p];
			x[p] = t;
			left++;
			p++;
		}
		else if(x[p]>0){
			int t = x[right];
			x[right] = x[p];
			x[p] = t;
			right--;			
		}
		else{
			_________________________;  //Code filling position
		}
	}
}

If an array is given:
25,18,-2,0,16,-5,33,21,0,19,-16,25,-3,0
After sorting:
-3,-2,-16,-5,0,0,0,21,19,33,25,16,18,25

Please analyze the code logic, infer the code at the underline, and submit it through the web page
Note: only take the missing code as the answer, and never fill in the redundant code, symbol or explanatory text!!

Answer: p++

7. Wrong note

A secret related unit has issued certain bills and wants to recover them all at the end of the year.

Each note has a unique ID No. of all bills throughout the year ID The number is continuous, but ID The start number of is randomly selected.

Due to the negligence of the staff, it is necessary to enter ID An error occurred on the th, causing a ID Broken sign, another one ID Duplicate sign.

Your task is to find the broken number through programming ID And duplicate ID. 

It is assumed that hyphenation cannot occur in the maximum and minimum numbers.

The program is required to first input an integer n (n < 100) to represent the number of subsequent data lines.
Then read in N rows of data.
The data length of each line is different, and it is several (no more than 100) positive integers (no more than 100000) separated by spaces
Each integer represents an ID number.

The program is required to output 1 line, including two integers m, N, separated by spaces.
Where, m represents the broken ID and n represents the duplicate ID

For example:
User input:
2
5 6 8 11 9
10 12 9

Program output:
7 9

Another example:
User input:
6
164 178 108 109 180 155 141 159 104 182 179 118 137 184 115 124 125 129 168 196
172 189 127 107 112 192 103 131 133 169 158
128 102 110 148 139 157 140 195 197
185 152 135 106 123 173 122 136 174 191 145 116 151 143 175 120 161 134 162 190
149 138 142 146 199 126 165 156 153 193 144 166 170 121 171 132 101 194 187 188
113 130 176 154 177 120 117 150 114 183 186 181 100 163 160 167 147 198 111 119

Program output:
105 120

Resource agreement:
Peak memory consumption (including virtual machine) < 64M
CPU consumption < 2000ms

Please output in strict accordance with the requirements, and do not print superfluous contents such as "please input...".

All code is placed in the same source file. After debugging, the copy is submitted to the source code.
Note: do not use package statements. Do not use features of jdk1.6 and above.
Note: the name of the Main class must be: Main, otherwise it will be treated as an invalid code.

##There is no answer to this question. It will be released later

8. Lucky numbers

The lucky number was named by the Polish mathematician Ulam. It uses the "sieve method" similar to generating prime numbers.

First write the natural number 1 from 1,2,3,4,5,6,....

1 Is the first lucky number.
Let's start with the number 2. Delete all items whose serial number can be divided by 2 and change to:

1 _ 3 _ 5 _ 7 _ 9 ....

Tighten them and re order them as follows:

1 3 5 7 9 .... . At this time, 3 is the second lucky number, and then delete all the numbers of serial number positions that can be divided by 3. Note that it is the sequence number position, not whether the number itself can be divided by 3!! Deleted should be 5, 11, 17, ...

At this time, 7 is the third lucky number, and then delete the number position that can be divided by 7(19,39,...) 

The last remaining sequence is similar:

1, 3, 7, 9, 13, 15, 21, 25, 31, 33, 37, 43, 49, 51, 63, 67, 69, 73, 75, 79, ...

This question requires:

Enter two positive integers m n separated by spaces (m < n < 1000 * 1000)
The program outputs the number of lucky numbers between M and n (excluding m and N).

For example:
User input:
1 20
Program output:
5

For example:
User input:
30 69
Program output:
8

Resource agreement:
Peak memory consumption (including virtual machine) < 64M
CPU consumption < 2000ms

Please output in strict accordance with the requirements, and do not print superfluous contents such as "please input...".

All code is placed in the same source file. After debugging, the copy is submitted to the source code.
Note: do not use package statements. Do not use features of jdk1.6 and above.
Note: the name of the Main class must be: Main, otherwise it will be treated as an invalid code.

import java.util.ArrayList;
import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
//		long a = System.currentTimeMillis();
		Scanner in = new Scanner(System.in);
		int m = in.nextInt();
		int n = in.nextInt();
		ArrayList<Integer> list = new ArrayList<>();
		//The first lucky number is 2 to filter
		for (int i = 1; i <= n; i++) {
			if (i % 2 != 0) {
				list.add(i);
			}
		}
		//Index is the lucky number index, and the second position is the second lucky number
		int index = 1;
		int presize = 0;
		// 1,3,5,7,9 => /3
		while (true) {
			int x = list.get(index);
			
			//Beyond the specified scope of the topic, terminate
			if (x >= n)break;
			//Record the size of the list before filtering
			presize = list.size();

			for (int i = 0; i < list.size(); i++) {
				if ((i + 1) % x == 0) {
					//Instead of directly removing it, set it to - 1 and then remove it in the following for loop because immediate removal will affect the size of the list, resulting in the continuous reduction of the size of the list and the number of errors removed
					list.set(i, -1);
				}

			}
			for (int i = 0; i < list.size(); i++) {
				if (list.get(i) == -1) {
					list.remove(i);
				}
			}
			//If the size after filtering is equal to that before filtering, the filtering is completed and terminated
			if (presize == list.size())break;
			//Lucky number index + 1
			index++;

		}
		int count = 0;
		//Count the number of lucky numbers in a given range
		for(int i=0;i<list.size();i++) {
			if(list.get(i)>m && list.get(i)<n)count++;
		}
		System.out.println(count);
//		System.out.println((System.currentTimeMillis()-a)+"ms");
	}
}

9. With score

100 It can be expressed as a fractional form: 100 = 3 + 69258 / 714

It can also be expressed as: 100 = 82 + 3546 / 197

Note: in the band fraction, the number is 1~9 Appear separately and only once (excluding 0).

Like this band fraction, 100 has 11 representations.

Title Requirements:
Read a positive integer n (n < 1000 * 1000) from the standard input
The program outputs the number, and the numbers 1 ~ 9 are used to form all kinds of numbers represented by fractions without repetition or omission.
Note: it is not required to output each representation, only the number of representations is counted!

For example:
User input:
100
Program output:
11

Another example:
User input:
105
Program output:
6

Resource agreement:
Peak memory consumption (including virtual machine) < 64M
CPU consumption < 3000ms

Please output in strict accordance with the requirements, and do not print superfluous contents such as "please input...".

All code is placed in the same source file. After debugging, the copy is submitted to the source code.
Note: do not use package statements. Do not use features of jdk1.6 and above.
Note: the name of the Main class must be: Main, otherwise it will be treated as an invalid code.

import java.util.LinkedList;
import java.util.Scanner;

public class Main {
	public static LinkedList<Integer> list = new LinkedList<>();
	public static int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	public static int N;
	public static int count = 0;

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		N = in.nextInt();			
		backtracing(9);
		System.out.println(count);
	}
//9-digit combination of 1-9 using backtracking
	public static void backtracing(int k) {
		if (list.size() == k) {
			// processing logic  
			//N < 1000 * 1000 is less than 7 digits. If I < = 6 is selected here and 5 is not selected, it is considered that num3 has at least one digit. If I < = 5 and j < = 6, num3 has at least two digits, and the condition of meeting one digit may be missed
			for(int i=0;i<=6;i++){
				int num1 = ToInt(0, i+1);
				for(int j=i+1;j<=7;j++) {
					int num2 = ToInt(i+1, j+1);
					int num3 = ToInt(j+1, arr.length);
					if(num1==82) {
//						System.out.println("num1:"+num1+", num2:"+num2+",num3:"+num3);
					}
					if(num2%num3==0 && (num1+num2/num3==N))count++;
				}
			}

		}
		for (int i = 0; i < arr.length; i++) {
			if (list.contains(arr[i]))
				continue;
			list.add(arr[i]);
			backtracing(k);
			list.removeLast();
		}
	}
	//Convert list [start, end] to an integer
	public static int ToInt(int start,int end) {
		int res = list.get(start);
		
		for(int i=start+1;i<end;i++) {
			res=res*10+list.get(i);
		}
		return res;
	}
}

10. Number of consecutive intervals

Xiao Ming has been thinking about such a strange and interesting question these days:

At 1~N How many consecutive intervals are there in a full permutation of? The definition of the consecutive interval here is:

If interval[L, R] All elements in (i.e. the second element of this arrangement) L From to R (elements) after incremental sorting, you can get a length of R-L+1 The "continuous" sequence of numbers is called this interval.

When N When he was very young, Xiao Ming could work out the answer quickly, but when N When it gets bigger, the problem is not so simple. Now Xiao Ming needs your help.

Input format:
The first line is a positive integer n (1 < = n < = 50000), indicating the scale of the full arrangement.
The second line is N different numbers PI (1 < = Pi < = N), indicating a full arrangement of these N numbers.

Output format:
Output an integer representing the number of different consecutive intervals.

Example:
User input:
4
3 2 4 1

The program shall output:
7

User input:
5
3 4 2 5 1

The program shall output:
9

Explanation:
In the first use case, there are seven consecutive intervals: [1,1], [1,2], [1,3], [1,4], [2,2], [3,3], [4,4]
In the second use case, there are 9 consecutive intervals: [1,1], [1,2], [1,3], [1,4], [1,5], [2,2], [3,3], [4,4], [5,5]

Resource agreement:
Peak memory consumption (including virtual machine) < 64M
CPU consumption < 5000ms

Please output in strict accordance with the requirements, and do not print superfluous contents such as "please input...".

All code is placed in the same source file. After debugging, the copy is submitted to the source code.
Note: do not use package statements. Do not use jdk1.6 and above features.
Note: the name of the Main class must be: Main, otherwise it will be treated as an invalid code.

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		int[] nums = new int[N];
		
		for (int i = 0; i < nums.length; i++) {
			nums[i] = in.nextInt();
		}
		int count = 0;
		for (int i = 0; i < nums.length; i++) {
			int min = nums[i];
			int max = nums[i];
			for (int j = i; j < nums.length; j++) {
				if(nums[j]>max) {
					max = nums[j];
				}
				
				if(nums[j]<min) {
					min = nums[j];
				}
				
				if((max-min+1)==(j-i+1)) {
					count++;
				}
			}
		}
		System.out.println(count);
	}
}	

Posted on Sat, 04 Dec 2021 23:09:43 -0500 by baze