2018 Ninth Blue Bridge Cup National Competition (Software) Test Question Group A

1. Area of ​​a triangle

The coordinates of the three vertices of the known triangle in the Cartesian coordinate system are:
(2.3, 2.5)
(6.4, 3.1)
(5.1, 7.2)

Find the area of ​​this triangle.

Note that what is being submitted is a floating point number represented as a decimal.
It is required to be accurate to 3 decimal places. If it is less than 3 digits, it needs to be filled with zeros.
——————————————————————————————————————————————————— ————————
Method 1: Directly set the coordinates of formula
A, B, C to A(x1, y1), B(x2, y2), C(x3, y3), then
S=(x1y2- x1y3+x2y3-x2y1+x3y1-x2y2)
I believe that few people can think of this in the examination room, so let's honestly use the following
method 2:
Find the length of ac, and then find the vertical distance from b to the line segment ac, Use base * height / 2 to find the area.
insert image description here
Let the equation of the straight line ac be y = ax + b, and bring in the coordinates of points a and c to obtain the coefficients a, b.
Bring the coordinates of point b into the distance formula from the point to the line.
insert image description here

public class Main {
    
    

	public static void main(String[] args) {
    
    
		double a = -41.0 / 13;
		double b = 7.2 - a * 5.1;
		double h = Math.abs(a * 2.3 - 2.5 + b) / Math.sqrt(a * a + 1);//b到ac的距离
		double di = Math.sqrt(Math.pow(5.1 - 6.4, 2) + Math.pow(7.2 - 3.1, 2));//ac的长
		double area = di * h / 2;
		System.out.printf("%.3f", area);
	}
}

Answer: 8.795

2. Parade phalanx

Country X wants to participate in the Allied military parade.
The organizer requires that the soldiers sent by each allied country can form exactly 2 phalanxes.
Country x found that the weak country y sent a team of 130 soldiers, and their soldiers could change 2 formations during the march:
130 = 81 + 49 = 9^2 + 7^2
130 = 121 + 9 = 11^2 + 3^2

King X was very excited, and felt that the area of ​​country X was 6 times that of country Y, so he should have formed more formations.
So he gave the order:
We're going to send a team, and we're going to make 12 formations on the way! ! !

The men are miserable, and they are busy calculating at least how many people can form 12 different two-sided formations.
Please take advantage of the computer to calculate the minimum number of soldiers needed.

(ps: Don't lose confidence, 1105 people can form 4 formations)

Note that what needs to be submitted is an integer, indicating the minimum number of soldiers required, do not fill in any redundant content.

public class Main {
    
    

	public static void main(String[] args) {
    
    
		for (int i = 1105; i < 999999; i++) {
    
    //最开始试的9999和99999,都没有符合的数
			int cnt = f(i);
			if (cnt == 12) {
    
    
				System.out.println(i);
				return ;
			}
		}
		//System.out.println(f(130));
		//System.out.println(f(1105));
	}

	private static int f(int num) {
    
    
		int cnt = 0;
		for (int a = 1; a * a < num; a++) {
    
    //一开始写的 a<num 和 b<num,导致很久算不出结果
			for (int b = a + 1; b * b < num; b++) {
    
    
				if (a * a + b * b == num) cnt++;
			}
		}
		return cnt;
	}
}

joseph ring

The numbers of n persons are 1~n. If they are arranged in a circle clockwise according to the number, the number is counted clockwise from the person with the number 1.
(The number of reports starts from 1) When k is reported, the person quits the game circle. The next person starts counting from 1 again.
Find the number of the last remaining person. This is the famous Joseph ring problem.

This problem is to find the number of the last remaining person given n and k.

The input of the question is one line, and the integers n and k separated by 2 spaces are
required to output an integer, indicating the number of the last remaining person.

Convention: 0 < n, k < 1 million

For example enter:
10 3

The program should output:
4

Resource convention:
peak memory consumption (including virtual machine) < 256M
CPU consumption < 1000ms

Please output strictly according to the requirements, and do not superficially print superfluous content like: "Please enter...".

Note: The
main function needs to return 0;
only use the ANSI C/ANSI C++ standard;
do not call special functions that depend on the compilation environment or operating system.
All dependent functions must be explicitly #included in the source file
. Common header files cannot be omitted through project settings.

When submitting your program, take care to select the desired language type and compiler type.
——————————————————————————————————————————————————— ————
I am using the array simulation, the optimal solution version of useful mathematical calculations on the Internet

import java.util.Scanner;

public class Main {
    
    
	static int[] cnt;//cnt[i]:i 的报数
	static boolean[] dead;//dead[i]:i 死了吗

	public static void main(String[] args) {
    
    
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		sc.close();
		
		cnt = new int[n + 5];
		dead = new boolean[n + 5];
		int num = 1, index = 1;//num是要报的数,index是当前下标
		int live = n;//活人数
		while (live > 1) {
    
    
			while (dead[index]) {
    
    
				index++;
				if (index == n + 1) index = 1;
				continue;
			}
			cnt[index] = num;
			if (cnt[index] == k) {
    
    
				dead[index] = true;
				live--;
				num = 1;
			} else {
    
    
				num++;
			}
			index++;
			if (index == n + 1) index = 1;
		}
		for (int i = 1; i <= n; i++) {
    
    
			if (!dead[i]) {
    
    
				System.out.println(i);
				break;
			}
		}
	}
}

self-describing sequence

Xiao Ming is studying a sequence, called the Golomb self-describing sequence, which may be denoted as {G(n)}. This sequence has 2 interesting properties:

  1. For any positive integer n, n occurs exactly G(n) times in the entire sequence.
  2. This sequence is not descending.

Here are the first few terms of {G(n)}:

n 1 2 3 4 5 6 7 8 9 10 11 12 13
G(n) 1 2 2 3 3 4 4 4 5 5 5 6 6

Given an integer n, can you help Xiao Ming calculate the value of G(n)?
【Input】
An integer n.
For 30% of the data, 1 <= n <= 1000000
For 70% of the data, 1 <= n <= 1000000000
For 100% of the data, 1 <= n <= 20000000000000000
[Output]
an integer G(n)
[like Example input]
13
[Sample output]
6
Resource convention:
Peak memory consumption (including virtual machine) < 256M
CPU consumption < 1000ms
Please output strictly according to the requirements, and do not superficially print superfluous content like: "Please enter...".
All code is placed in the same source file, after debugging, copy and submit the source code.
Don't use the package statement. Do not use features of jdk1.7 and above.
The name of the main class must be: Main, otherwise it will be processed as invalid code.
——————————————————————————————————————————————————— —

import java.util.Scanner;

public class Main {
    
    
	static int[] dp;

	public static void main(String[] args) {
    
    
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.close();
		
		dp = new int[n + 15];//n超出int范围时不适用
		dp[1] = 1;
		for (int i = 2; i <= n; i++) {
    
    
			dp[i] = 1 + dp[i - dp[dp[i - 1]]];
		}
		System.out.println(dp[n]);
	}
}

Guess you like

Origin blog.csdn.net/QinLaoDeMaChu/article/details/109341422