Question 1:
Number of coal balls
There's a bunch of coal balls, piled up in triangular pyramids. Specifically:
Place one on the first floor.
There are three layers in the second layer (arranged in triangles).
Six on the third floor (arranged in triangles)
Layer 4, 10 (arranged in triangles)
....
If there are 100 layers, how many coal balls are there?
Please fill in the figures indicating the total number of coal balls.
Note: Your submission should be an integer, do not fill in any superfluous content or explanatory text.
Code
public class Main {
public static void main(String[] args) {
int sum = 0,temp = 0;
for(int i = 1 ; i <= 100; i++){
temp += i;
sum += temp;
}
System.out.println(sum);
}
}
output
171700
Question 2
Birthday candle
A gentleman has held a birthday party every year since a certain year, and each time he has to blow out candles of the same number as his age.
Now, counting it out, he blew out 236 candles altogether.
Excuse me, how old did he start his birthday party?
Please fill in the age at which he started his birthday party.
Note: Your submission should be an integer, do not fill in any superfluous content or explanatory text.
Code
public class Main {
public static void main(String[] args) {
for(int i = 1;;i++){
int sum = 0;
for(int j = i;;j++){
sum += j;
if(sum >= 236)
break;
}
if(sum == 236){
System.out.println(i);
break;
}
}
}
}
output
26
Question 3
Formula
In this formula, A~I represents numbers 1~9 and different letters represent different numbers.
For example:
6+8/3+952/714 is a solution.
5+3/1+972/486 is another solution.
How many solutions are there in this formula?
Note: Your submission should be an integer, do not fill in any superfluous content or explanatory text.
Thoughts on Problem Solving
Violent solution can be used, or deep search can be used. Deep search is recommended.
Answer
public class Main {
static int sum = 0;
static int[] a = new int[10];
static int[] check = new int[10];
public static void main(String[] args) {
dfs(1);
System.out.println(sum);
}
private static void dfs(int k) {
//When the numbers are all right
if(k==10){
int A = a[1];
int B = a[2];
int C = a[3];
int DEF = a[4]*100+a[5]*10+a[6];
int GHI = a[7]*100+a[8]*10+a[9];
//Convert division into multiplication to avoid the problem of accuracy
if((B*GHI+C*DEF) == (10-A)*(C*GHI))
sum++;
}
//Using Deep Search to Fill in Numbers
for(int i = 1;i <= 9;i++){
if(check[i] == 1)
continue;
a[k] = i;
check[i] = 1;
dfs(k+1);//Deep search
check[i] = 0;//To flash back
}
}
}
output
29
Question 4
Sub-group
Nine athletes participate in the competition and need to be divided into three groups for preliminary competition.
What are the grouping schemes?
We label athletes A,B,C,... I
The following program lists all the grouping methods.
The normal output of the program is:
ABC DEF GHI
ABC DEG FHI
ABC DEH FGI
ABC DEI FGH
ABC DFG EHI
ABC DFH EGI
ABC DFI EGH
ABC DGH EFI
ABC DGI EFH
ABC DHI EFG
ABC EFG DHI
ABC EFH DGI
ABC EFI DGH
ABC EGH DFI
ABC EGI DFH
ABC EHI DFG
ABC FGH DEI
ABC FGI DEH
ABC FHI DEG
ABC GHI DEF
ABD CEF GHI
ABD CEG FHI
ABD CEH FGI
ABD CEI FGH
ABD CFG EHI
ABD CFH EGI
ABD CFI EGH
ABD CGH EFI
ABD CGI EFH
ABD CHI EFG
ABD EFG CHI
... (omitted below, 560 lines in total).
public class Main
{
public static String remain(int[] a)
{
//The last three letters
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)
{
//Choose three of the remaining six letters
for(int i=0; i<a.length; i++){//Fourth letter
if(a[i]==1) continue;
a[i] = 1;
for(int j=i+1; j<a.length; j++){//Fifth letter
if(a[j]==1) continue;
a[j]=1;
for(int k=j+1; k<a.length; k++){//Sixth letter
if(a[k]==1) continue;
a[k]=1;
System.out.println(__________________________________); //Fill-in position
a[k]=0;
}
a[j]=0;
}
a[i] = 0;
}
}
public static void main(String[] args)
{
//Set up a 9-letter integer array to mark which letter has been used
int[] a = new int[9];
a[0] = 1;//First letter
for(int b=1; b<a.length; b++){
a[b] = 1;//The second letter
for(int c=b+1; c<a.length; c++){
a[c] = 1;//The third letter
String s = "A" + (char)(b+'A') + (char)(c+'A');//Select the first group with A and store it in string s
f(s,a);//Three out of the remaining six
a[c] = 0;//Clearance markers
}
a[b] = 0;//Clearance markers
}
}
}
Read the code carefully and fill in what is missing in the underlined section.
Note: Do not fill in any existing content or explanatory text.
Fill in the blanks
s + " " + (char)(i+'A') + (char)(j+'A') + (char)(k + 'A') + " " + remain(a)
Question 5
draw
Planet X is sending a five-member observer mission to Planet W.
Among them:
Country A can send up to four people.
Country B can send up to two people.
Country C can send up to two people.
....
So how many different combinations of countries will eventually be sent to W?
The following program solves this problem.
Array a [] is the largest number of places that each country can send.
The results of program execution are as follows:
DEFFF
CEFFF
CDFFF
CDEFF
CCFFF
CCEFF
CCDFF
CCDEF
BEFFF
BDFFF
BDEFF
BCFFF
BCEFF
BCDFF
BCDEF
....
(omitted below, 101 rows in total)
public class Main
{
public static void f(int[] a, int k, int n, String s)
{
if(k==a.length){
if(n==0) System.out.println(s);
return;
}
String s2 = s;
for(int i=0; i<=a[k]; i++){
_____________________________; //Fill-in position
s2 += (char)(k+'A');
}
}
public static void main(String[] args)
{
int[] a = {4,2,2,1,1,3};
f(a,0,5,"");
}
}
Read the code carefully and fill in what is missing in the underlined section.
Note: Do not fill in any existing content or explanatory text.
Fill in the blanks
f(a, k+1, 5-s2.length(),s2)
Sixth topic
Square filling
The following 10 grids:
Fill in the numbers 0 to 9. Requirement: Two consecutive numbers cannot be adjacent.
(Right, left, right, right and left, diagonally adjacent)
How many possible fill-in schemes are there?
Please fill in the integer indicating the number of alternatives.
Note: Your submission should be an integer, do not fill in any superfluous content or explanatory text.
Thoughts on Problem Solving
Because it is a filling-in problem, it can be filled in directly by violence or deep search.
Code
public class Main
{
static int sum = 0;
static int[] a = new int[10];
static int[] check = new int[10];
public static void main(String[] args)
{
dfs(0);
System.out.println(sum);
}
private static void dfs(int k) {
if(k == 10){
if(Math.abs(a[0]-a[1])>1 && Math.abs(a[0]-a[3])>1 && Math.abs(a[0]-a[4])>1 && Math.abs(a[0]-a[5])>1 &&
Math.abs(a[1]-a[2])>1 && Math.abs(a[1]-a[4])>1 && Math.abs(a[1]-a[5])>1 && Math.abs(a[1]-a[6])>1 &&
Math.abs(a[2]-a[5])>1 && Math.abs(a[2]-a[6])>1 &&
Math.abs(a[3]-a[4])>1 && Math.abs(a[3]-a[7])>1 && Math.abs(a[3]-a[8])>1 &&
Math.abs(a[4]-a[5])>1 && Math.abs(a[4]-a[7])>1 && Math.abs(a[4]-a[8])>1 && Math.abs(a[4]-a[9])>1 &&
Math.abs(a[5]-a[6])>1 && Math.abs(a[5]-a[8])>1 && Math.abs(a[5]-a[9])>1 &&
Math.abs(a[6]-a[9])>1 &&
Math.abs(a[7]-a[8])>1 &&
Math.abs(a[8]-a[9])>1){
sum++;
}
}
for(int i = 0;i<10;i++){
if(check[i] == 1)
continue;
check[i] = 1;
a[k] = i;
dfs(k+1);
check[i] = 0;
}
}
}
Fill in the blanks
1580
Topic 7
Stamp cutting
For example, [Figure 1.jpg], there are 12 stamps linked together for the 12 zodiac signs.
Now you're going to cut five out of them. The requirements must be connected. (Connecting only one corner is not connected).
For example, [fig. 2.jpg], [fig. 3.jpg], the pink part is a qualified cut.
Please calculate how many different cutting methods there are.
Please fill in the integer indicating the number of alternatives.
Note: Your submission should be an integer, do not fill in any superfluous content or explanatory text.
Code
public class Main {
static int[][] check = new int[3][4];
static int[] a = new int[12];
static int sum;
public static void main(String[] args) {
for(int i=0;i<12;i++)
for(int j=i+1;j<12;j++)
for(int k=j+1;k<12;k++)
for(int x=k+1;x<12;x++)
for(int y=x+1;y<12;y++){
check[i/4][i%4] = 1;
check[j/4][j%4] = 1;
check[k/4][k%4] = 1;
check[x/4][x%4] = 1;
check[y/4][y%4] = 1;
f();
check[i/4][i%4] = 0;
check[j/4][j%4] = 0;
check[k/4][k%4] = 0;
check[x/4][x%4] = 0;
check[y/4][y%4] = 0;
}
System.out.println(sum);
}
private static void f() {
boolean flag = true;
loopA:
for(int i = 0;i<3;i++)
for(int j=0;j<4;j++)
if(check[i][j] == 1){
dfs(i,j);
break loopA;
}
loopB:
for(int i = 0;i<3;i++)
for(int j=0;j<4;j++)
if(check[i][j] == 1){
flag = false;
break loopB;
}
if(flag)
sum++;
}
private static void dfs(int x,int y) {
for(int i = -1;i<=1;i++){
int dy = i + y;
if(dy>=0&&dy<4&&check[x][dy] == 1){
check[x][dy] = 0;
dfs(x, dy);
}
int dx = i + x;
if(dx>=0&&dx<3&&check[dx][y] == 1){
check[dx][y] = 0;
dfs(dx, y);
}
}
}
}
Fill in the blanks
116
Question 8
Sum of squares
Four-square theorem, also known as Lagrange theorem:
Each positive integer can be expressed as the sum of squares of up to four positive integers.
If 0 is included, it can be expressed as the sum of squares of four numbers.
For example:
5 = 0^2 + 0^2 + 1^2 + 2^2
7 = 1^2 + 1^2 + 1^2 + 2^2
(^ Sign means multiplier)
For a given positive integer, there may be multiple representations of the sum of squares.
Ask you to sort four numbers:
0 <= a <= b <= c <= d
All possible representations are arranged in ascending order of joint primary keys by a, b, C and D. Finally, the first representation is output.
Program input is a positive integer N (N < 5000000)
Four non-negative integers are required to be output, sorted from small to large, separated by spaces in the middle.
For example, input:
5
Then the program should output:
0 0 1 2
For example, input:
12
Then the program should output:
0 2 2 2
For example, input:
773535
Then the program should output:
1 1 267 838
Resource agreements:
Peak memory consumption (including virtual machines) < 256M
CPU consumption < 3000ms
Please output strictly according to the requirements, and do not print something like "Please input..." Excess content.
Answer
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int num = s.nextInt();
int temp = (int) Math.sqrt(num);
for(int i = 0;i<=temp;i++)
for(int j = i;j<=temp;j++)
for(int k = j;k<=temp;k++)
for(int t = k;t<=temp;t++){
if(i*i+j*j+k*k+t*t == num){
System.out.println(i+" "+j+" "+k+" "+t);
return;
}
}
}
}
Question 9
Ball-taking Game
Two people play the game of catching the ball.
There are a total of N balls, each person takes turns to take the ball, each time can take any number of sets {n1,n2,n3}.
If you can't continue to get the ball, the game is over.
At this point, the side with odd balls wins.
If both are odd, it's a draw.
Assuming that both sides adopt the smartest approach,
Is the first person to score a goal sure to win?
Trial programming solves this problem.
Input format:
The first row has three positive integers n1 n2 n3, separated by spaces, indicating the number of desirable numbers per time (0 < n1,n2,n3 < 100)
The second line has five positive integers X1 x 2.. x5, space separated, indicating the initial number of balls in five innings (0 < xi < 1000)
Output format:
Five characters in a row, separated by spaces. Individually, it indicates whether the person who takes the ball first in each game can win.
If you can win, you will output +, and then if you can equalize your opponent and output 0, you will lose anyway.-
For example, input:
1 2 3
1 2 3 4 5
The program should output:
+ 0 + 0 -
For example, input:
1 4 5
10 11 12 13 15
The program should output:
0 - 0 + +
For example, input:
2 3 5
7 8 9 10 11
The program should output:
+ 0 0 0 0
Resource agreements:
Peak memory consumption (including virtual machines) < 256M
CPU consumption < 3000ms
Answer
import java.util.Scanner;
public class Main {
public static int[] value = new int[1000];
public static int[] getN = new int[3];
public static int[] init = new int[5];
public static char[] result = {'-','0','0','+'};
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
for(int i = 0;i < 3;i++)
getN[i] = in.nextInt();
for(int i = 0;i < 5;i++)
init[i] = in.nextInt();
int minN = Math.min(getN[0], Math.min(getN[1], getN[2]));
if(minN % 2 == 1) //Whether the draw is odd or even is my guess.
value[0] = 1; //It represents a draw; both default to odd numbers.
else
value[0] = 2; //Represents a draw; both default to even numbers.
for(int i = 1;i < minN;i++)
value[i] = 2; //0 for negative, 1 and 2 for draw, 3 for victory
for(int i = minN;i < 1000;i++) {
int temp = 0; //Initialization, the current i balls, the first to lose
for(int j = 0;j < 3;j++) {
if(i - getN[j] < 0)
continue;
if(i - getN[j] == 0 && getN[j] % 2 == 1)
temp = 3;
if(value[i - getN[j]] == 0) { //Represents i - getN[j] when the ball is taken first, it must lose
if(getN[j] % 2 == 0)
temp = 3;
//At this point, the final result is that the balls taken by two people are even, but if temp takes another of the three numbers
//If it's a win-win outcome, it's a draw.
else
temp = 2 > temp ? 2 : temp;
}
if(value[i - getN[j]] == 1) { //When i - getN[j] balls are taken first, the number of balls taken by both players is odd.
if(getN[j] % 2 == 0)
temp = 1 > temp ? 1 : temp; //For comparison, see ibid.
}
if(value[i - getN[j]] == 2) {//Represents that when i - getN[j] balls are taken first, both of them have even numbers of balls.
if(getN[j] % 2 == 1)
temp = 3; //If such a situation arises, it will win. There is no need to make a comparative judgement.
else
temp = 2 > temp ? 2 : temp; //Compare here with the above, excluding the situation that must be lost
}
if(value[i - getN[j]] == 3) {//Represents that if i - getN[j] takes the ball first, it will win.
if(getN[j] % 2 == 1)
temp = 1 > temp ? 1 : temp;
}
}
value[i] = temp; //For the current one, the winner wins or loses.
}
//Print the final result of the title
for(int i = 0;i < 5;i++)
System.out.print(result[value[init[i]]]+" ");
}
}
Topic 10
Compression transformation
Xiao Ming is currently studying compression algorithms.
He knows that if we can make the value very small, we can get a higher compression ratio by entropy coding.
However, minimizing the number is a challenge.
Recently, Xiao Ming needs to compress some sequences of positive integers. These sequences are characterized by the fact that the numbers appearing later are probably the ones that have just appeared. For this particular sequence, Xiao Ming is going to make a transformation of the sequence to reduce the value of the number.
The transformation process is as follows:
From left to right enumeration sequence, each enumeration to a number, if the number has not appeared, just converted the number into its opposite number, if the number has appeared, then see it in the original sequence after the last appearance (and in front of the current number) there are several numbers, with this kind of number to replace the original number.
For example, the sequence (a1, a2, a3, a4, a5)=(1, 2, 2, 1, 2) in the transformation process is:
a1: 1 has not appeared, so a1 becomes - 1;
a2: 2 did not appear, so a2 changed to - 2.
a3: 2 appeared, the last time was a2 of the original sequence, after a2 and before a3, there were 0 kinds of numbers, so A3 changed to 0.
a4: 1 appeared, the last time was a1 of the original sequence, after a1 and before a4, there was a number, so A4 changed to 1.
a5: 2 appeared, the last time was a3 of the original sequence, after a3 and before a5 there was a number, so a5 changed to 1.
Now, the original sequence is given. What is the sequence transformed according to this transformation rule?
Input format:
The first line of input contains an integer n, representing the length of the sequence.
The second line contains n positive integers representing the input sequence.
Output format:
Output a line containing n numbers representing the transformed sequence.
For example, input:
5
1 2 2 1 2
The program should output:
-1 -2 0 1 1
For example, input:
12
1 1 2 3 2 3 1 2 2 2 3 1
The program should output:
-1 0 -2 -3 1 1 2 2 0 0 2 2
Data Size and Agreement
For 30% of the data, n<=1000;
For 50% of the data, n <=30000;
For 100% data, 1 <= n <= 100000, 1 <= AI <= 10 ^ 9
Resource agreements:
Peak memory consumption (including virtual machines) < 256M
CPU consumption < 3000ms
Please output strictly according to the requirements, and do not print something like "Please input..." Excess content.
Thoughts on Problem Solving
Finding the Type of Numbers by Using the Non-existent Characteristic of set Set Elements
Answer
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Main{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int len = s.nextInt();
int[] a = new int[len];
int[] result = new int[len];
for(int i = 0;i<len;i++)
a[i] = s.nextInt();
for(int i = 0;i<len;i++){
boolean isAppear = false;
int temp = -1;
for(int j = 0;j<i;j++){
if(a[j] == a[i]){
temp = j;
isAppear = true;
}
}
if(isAppear == false){
result[i] = -1*a[i];
}else{
Set<Integer> set = new HashSet<>();
for(int j=temp+1;j<i;j++)
set.add(a[j]);
result[i] = set.size();
}
}
for(int i =0;i<len;i++){
if(i != len-1)
System.out.print(result[i] + " ");
else
System.out.print(result[i]);
}
}
}