A. Number of briquettes
Title Description
This question is a blank filling question. You only need to calculate the result and use the output statement in the code to output the filled result.
There is a pile of coal balls, piled into a triangular pyramid. Specifically: 1 on the first floor, 3 on the second floor (arranged in a triangle), 6 on the third floor (arranged in a triangle), 10 on the fourth floor (arranged in a triangle),... If there are 100 floors in total, how many briquettes are there?
Topic analysis
It is observed that the number of briquettes in layer i is i*(i+1)/2
Problem solution
171700
/** * @Description * @Author:PrinceHan * @CreateTime:2022/2/5 08:33 */ public class Main { public static void main(String[] args) { int sum = 0; for (int i = 1; i <= 100; i++) { sum += i * (i + 1) / 2; } System.out.println(sum); } }
B. Birthday candle
Title Description
This question is a blank filling question. You only need to calculate the result and use the output statement in the code to output the filled result.
A gentleman has held a birthday party every year since a certain year, and he has to blow out the same number of candles as his age every time.
Now, he blew out 236 candles.
Excuse me, how old did he start his birthday party?
Please output the age of his birthday party.
Topic analysis
You can initialize the candle array first, find a continuous interval in the array and make the sum equal to 236. A window can be maintained. When the sum is less than 236, slide the right window. When the sum is greater than 236, slide the left window. When it is equal to 236, output the value of the left window, which is the required value.
Problem solution
26
/** * @Description * @Author:PrinceHan * @CreateTime:2022/2/5 08:39 */ public class B { public static void main(String[] args) { int sum = 0; int[] arr = new int[101]; for (int i = 1; i <= 100; i++) { arr[i] = i; } for (int i = 1, j = 1; i <= 100 && j <= 100; ) { if (sum < 236) sum += arr[j++]; if (sum == 236) { System.out.println(i); System.exit(0); } if (sum > 236) { sum -= arr[i]; i++; } } } }
C. Calculation formula
Title Description
This question is a blank filling question. You only need to calculate the result and use the output statement in the code to output the filled result.
B DEF A + --- + ------- = 10 C GHI
In this formula, A ~ I represents numbers from 0 to 9, and different letters represent different numbers.
For example, 6 + 8 / 3 + 952 / 7146 + 8 / 3 + 952 / 714 is a solution, and 5 + 3 / 1 + 972 / 4865 + 3 / 1 + 972 / 486 is another solution.
How many solutions are there to this equation?
Topic analysis
In essence, it is a problem of seeking complete arrangement. Deep search is used, which only deals with the elements of the array.
Problem solution
29
/** * @Description * @Author:PrinceHan * @CreateTime:2022/2/5 08:48 */ public class C { static int[] nums = new int[10]; static boolean[] vis = new boolean[10]; static int ans; public static void main(String[] args) { dfs(1); System.out.println(ans); } public static void dfs(int step) { if (step == 10 && check(nums)) { ans++; return; } for (int i = 1; i < 10; i++) { if (!vis[i]) { nums[step] = i; vis[i] = true; dfs(step + 1); vis[i] = false; } } } public static boolean check(int[] a) { int num1 = a[3]; int num2 = a[4] * 100 + a[5] * 10 + a[6]; int num3 = a[7] * 100 + a[8] * 10 + a[9]; if (a[1] * num1 * num3 + a[2] * num3 + num2 * num1 == 10 * num1 * num3) return true; return false; } }
D. Sub group
Title Description
This is a code completion question.
Nine athletes participate in the competition and need to be divided into three groups for preliminary competition. What are the grouping schemes?
We mark athletes as A,B,C,... I.
The following procedure lists all grouping methods.
source code
import java.util.*; public class Main { public static String remain(int[] a) { String s = ""; for(int i=0; i<a.length; i++){ if(a[i] == 0) s += (char)(i+'A'); } return s; } public static void f(String s, int[] a) { for(int i=0; i<a.length; i++){ if(a[i]==1) continue; a[i] = 1; for(int j=i+1; j<a.length; j++){ if(a[j]==1) continue; a[j]=1; for(int k=j+1; k<a.length; k++){ if(a[k]==1) continue; a[k]=1; System.out.println(____________________________); a[k]=0; } a[j]=0; } a[i] = 0; } } public static void main(String[] args) { int[] a = new int[9]; a[0] = 1; for(int b=1; b<a.length; b++){ a[b] = 1; for(int c=b+1; c<a.length; c++){ a[c] = 1; String s = "A" + (char)(b+'A') + (char)(c+'A'); f(s,a); a[c] = 0; } a[b] = 0; } } }
Topic analysis
This question is a code blank filling question, which has certain problem-solving skills. First of all, the functions or variables given in the code will be used. You can use the break point to debug how the program runs. The first three letters are obtained in the main() method, and s is passed to the f() method through the parameter. It is realized here that the string may be spliced. The f() method is to obtain the string with the middle three digits. The string containing the remaining letters is obtained through the remain() method. The string can be spliced and printed.
Problem solution
System.out.println(s + " " + (char) (i + 'A') + (char) (j + 'A') + (char) (k + 'A') + " " + remain(a));
E. Draw lots
Title Description
This topic is code completion. Please complete the source code given in the topic.
Planet X will send a five member observation mission to planet W.
Of which:
Country A can send up to four people.
Country B can send up to 2 people.
Country C can send up to 2 people.
...
So how many different combinations of countries will there be in the final observation mission to planet W?
The following procedure solves this problem. Array a [] is the largest number of places that each country can send. The program execution result is:
DEFFF CEFFF CDFFF CDEFF CCFFF CCEFF CCDFF CCDEF BEFFF BDFFF BDEFF BCFFF BCEFF BCDFF BCDEF .... (Omitted below, 101 lines in total)
The following code is for this purpose. Please read the source code carefully and fill in the missing code in the underlined part.
source code
import java.util.*; public class Main { public static void f(int[] a, int k, int n, String s) { if(k==a.length){ //Considering all countries if(n==0) System.out.println(s);//n==0 indicates that 5 people have been taken return; } String s2 = s;//Save the selected combination for(int i=0; i<=a[k]; i++){ ______________________; s2 += (char)(k+'A'); } } public static void main(String[] args) { int[] a = {4,2,2,1,1,3}; f(a,0,5,""); } }
Topic analysis
Observe the f() method, there is an exit, and realize that recursion may be used. It is observed that in the main() method, the f() method passes parameters k = 0,n = 5, and the recursive end condition is Ka Length and N0 make K increase and N decrease. These operations should be completed in the blank of the code.
for (int i = 0; i <= a[k]; i++) { f(a, k+1,n-i,s2);//k+1 visits the next country, and n-i means to subtract the number of people selected from the current country s2 += (char) (k + 'A'); }
F. Square filling number
Title Description
This question is a blank filling question. You only need to calculate the result and use the output statement in the code to output the filled result.
The following 10 grids:
+--+--+--+ | | | | +--+--+--+--+ | | | | | +--+--+--+--+ | | | | +--+--+--+
Fill in numbers from 0 to 9. Requirement: two consecutive numbers cannot be adjacent. (left, right, up, down and diagonal are adjacent)
How many possible filling schemes are there?
Topic analysis
In essence, it is a full arrangement, but it is required that two consecutive numbers are not adjacent. Non adjacent means that the absolute value of the difference is not equal to 1. Just judge.
Problem solution
1580
/** * @Description * @Author:PrinceHan * @CreateTime:2022/2/6 09:41 */ public class F { static int[] nums = new int[10]; static boolean[] vis = new boolean[10]; static int ans; public static void main(String[] args) { dfs(0); System.out.println(ans); } public static void dfs(int step) { if (step == 10 && check(nums)) { ans++; return; } for (int i = 0; i < 10; i++) { if (!vis[i]) { nums[step] = i; vis[i] = true; dfs(step + 1); vis[i] = false; } } } public static boolean check(int[] a) { return Math.abs(a[0] - a[1]) != 1 && Math.abs(a[1] - a[2]) != 1 && Math.abs(a[3] - a[4]) != 1 && Math.abs(a[4] - a[5]) != 1 && Math.abs(a[5] - a[6]) != 1 && Math.abs(a[7] - a[8]) != 1 && Math.abs(a[8] - a[9]) != 1 && Math.abs(a[0] - a[4]) != 1 && Math.abs(a[1] - a[5]) != 1 && Math.abs(a[2] - a[6]) != 1 && Math.abs(a[3] - a[7]) != 1 && Math.abs(a[4] - a[8]) != 1 && Math.abs(a[5] - a[9]) != 1 && Math.abs(a[0] - a[3]) != 1 && Math.abs(a[1] - a[4]) != 1 && Math.abs(a[2] - a[5]) != 1 && Math.abs(a[0] - a[5]) != 1 && Math.abs(a[1] - a[6]) != 1 && Math.abs(a[3] - a[8]) != 1 && Math.abs(a[4] - a[9]) != 1 && Math.abs(a[4] - a[7]) != 1 && Math.abs(a[5] - a[8]) != 1 && Math.abs(a[6] - a[9]) != 1; } }
G. Cut stamps
As shown in [figure 1.jpg], there are 12 stamps of the 12 Chinese zodiac animals connected together.
Now you have to cut five of them. They must be connected.
(only connecting one corner is not connected.) for example, in [figure 2.jpg] and [figure 3.jpg], the part shown in pink is qualified cutting.
Please calculate how many different cutting methods there are.
Topic analysis
At the beginning, I wanted to search with dfs, but the number of schemes is less than the correct answer. After looking at the problem solution, I found that using dfs can not solve the "T" scheme. Referring to the problem solution on station B, Cut stamps.
Select 5 out of the 12 to initialize an array with a length of 12 and 5 ones. Fully arrange the array, and then judge whether the selected ones are connected. The connection problem can be handled with dfs. When performing the full arrangement, consider the repetition of the full arrangement and remove the duplication. There are ready-made full arrangement functions in C + +:
#include <algorithm> bool next_permutation(iterator start,iterator end)
You can get the full arrangement without repetition, but there is no full arrangement method in Java, which is generally realized by handwritten dfs.
Problem solution
116
/** * @Description * @Author:PrinceHan * @CreateTime:2022/2/6 10:20 */ public class G { static int[] a = {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1};//Stamp array static int[] dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1}; static boolean[] vis = new boolean[12]; static int ans; public static void main(String[] args) { int[] path = new int[12]; f(0, path); System.out.println(ans); } static void dfs(int[][] g, int x, int y) { g[x][y] = 0;//Mark part of 1 as 0 for (int i = 0; i < 4; i++) { int xx = x + dx[i]; int yy = y + dy[i]; if (in(xx, yy) && g[xx][yy] == 1) { dfs(g, xx, yy); } } } static boolean check(int[] path) { //The path array can be regarded as a container for storing the full array results, and so is the path array in the following int[][] g = new int[3][4];//g is the graph, indicating which piece is selected for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { if (path[i * 4 + j] == 1) g[i][j] = 1;//Corresponding to subscript else g[i][j] = 0; } } int cnt = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { if (g[i][j] == 1) { dfs(g, i, j);//If this is the selected piece, start deep search from this piece cnt++; } } } return cnt == 1;//A deep search proves China Unicom } //Full permutation function, path array can be regarded as a container for storing Full Permutation results static void f(int k, int path[]) { if (k == 12) { if (check(path)) ans++; } for (int i = 0; i < 12; i++) { //De duplication: if the current one is selected and the result of the current one is the same as that of the previous step, but the previous step has not been selected, skip this step if (i > 0 && a[i] == a[i - 1] && !vis[i - 1]) continue; if (!vis[i]) { vis[i] = true; path[k] = a[i]; f(k + 1, path); vis[i] = false; } } } static boolean in(int x, int y) { return x >= 0 && x < 3 && y >= 0 && y < 4; } }