Eighty-two, Java algorithm practice punch card (three questions)

Article directory

Topic 1

Topic description

Operational restrictions

answer

topic two

Topic description

Operational restrictions

answer

topic three

Topic description

enter description

output description

input sample

Sample output

Operational restrictions

answer


 

Topic 1

Topic description

age coincidence

This question is a fill-in-the-blank question. After calculating the result, use the output statement in the code to output the filled result.

Xiao Ming went to the movies with his cousin, and someone asked their age. Xiao Ming said: This year is our lucky year. The four digits of my birth year add up to exactly my age. So is the cousin's. It is known that this year is 2014, and the age mentioned by Xiao Ming refers to one year old.

Please infer and fill in Xiao Ming's birth year.


Operational restrictions

  • Maximum running time: 1s
  • Maximum running memory: 128M

answer

public class Main {
	public static void main(String[] args) {
		for (int i = 2014; i > 1950; i--) {
			if ((2014 - i) == (i % 10 + (i / 10) % 10 + (i / 100) % 10 + (i / 1000) % 10))
				System.out.println(i);
		}
	}
}

Note: Determine the interval (1950, 2014), the result is 2006, 1988, he is my cousin, so he was born in 2006

Answer: Me: 1988 He: 2006

topic two

Topic description

playing card triangle

This question is a fill-in-the-blank question. After calculating the result, use the output statement in the code to output the filled result.

A, 2, 3, 4, 5, 6, 7, 8, 9, a total of 9 cards are arranged in a regular triangle (A is counted as 1). The sum of each side is required to be equal. The figure below is an arrangement.

 

There may be many such arrangements.

If considering rotation and mirror image (symmetry), the same one is the same, how many different arrangements are there?

Please calculate and submit this number.


Operational restrictions

  • Maximum running time: 1s
  • Maximum running memory: 256M

answer

public class Main {
	static int ans = 0;
	static int[] A = new int[9];// 用来存数据
	static int[] B = new int[9];// 用来表示该数组的位置是否有被遍历过

	public static void main(String[] args) {
		dfs(0);

		// 因为旋转和镜像代表的是同一种,但是它们各重复三次,即重复六次,所以最后要除以六
		System.out.println(ans / 6);
	}

	public static void dfs(int num) {
		if (num == 9 && ((A[0] + A[1] + A[2] + A[3]) == (A[3] + A[4] + A[5] + A[6]))
				&& ((A[3] + A[4] + A[5] + A[6]) == (A[6] + A[7] + A[8] + A[0]))) {
			ans++;
			return;
		}
		for (int i = 0; i < 9; i++) {
			if (B[i] == 0) {
				A[num] = i + 1;// 对数组进行赋值
				B[i] = 1;// 表示该位置已经赋值
				dfs(num + 1);
				B[i] = 0;// 回溯
			}
		}
	}
}

Note: dfs search, full arrangement, and grasping the three elements are the most critical

  • Entry parameter settings
  • export settings for dfs
  • Whether backtracking is required

Answer: 144

topic three

Topic description

ball game

There are n balls in the box. Two people, A and B, take turns to take balls from the box. Each person can see how many balls the other person has taken, and how many are left in the box. They're all smart and don't make wrong judgments.

We agree:

The number of balls each person takes from the box must be: 1, 3, 7 or 8. You can't abstain when it's your turn to take the ball! A takes the ball first, and then alternately takes the ball until it is finished. The side forced to get the last ball is the loser (lose side)

Please program to determine whether A can win for a given initial number of balls without either side being wrong?


enter description

The first is an integer n (n<100), which means that there are n integers next.

Then there are n integers, each on a line (integer < 10^4), representing the initial number of balls.

output description

The program then outputs n lines, indicating whether A wins or loses (0 for loss, 1 for win).

input sample

4
1
2
10
18

Sample output

0
1
1
0

Operational restrictions

  • Maximum running time: 1s
  • Maximum running memory: 256M

answer

(violent solution)

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

//类似于爬楼梯的递归模式
public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		List<Integer> l = new ArrayList<>();
		for (int i = 0; i < n; i++) {
			l.add(scanner.nextInt());
		}
		for (int j = 0; j < n; j++) {
			if (A(l.get(j))) {
				System.out.println(1);
			} else {
				System.out.println(0);
			}
		}
	}

	public static boolean A(int m) {
		if (m >= 1) {
			switch (m) {
			case 1:
				return false;// 当你要去拿球时,并且最后只剩下1个球时,输
			case 3:
				return false;// 当你要去拿球时,并且最后只剩下3个球时,输
			case 7:
				return false;// 输
			case 8:
				return true;// 赢
			default:
				return (!A(m - 1) || !A(m - 3) || !A(m - 7) || !A(m - 8));// 这个时候表示自己拿1或3或7或8个球,然后这时对象变成了对面的一个人,所以要加!
			}
		}
		return false;
	}
}

(dp solve)

import java.util.Scanner;

//dp解法
public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		boolean[] A = new boolean[10009];// 因为n<10^4
		A[0] = true;
		for (int i = 1; i < 10009; i++) {
			A[i] = (!(A[i - 1]) || (i >= 3 && !A[i - 3]) || (i >= 7 && !A[i - 7]) || (i >= 8 && !A[i - 8]));// 这个时候表示自己拿1或3或7或8个球,然后这时对象变成了对面的一个人,所以要加!
		}
		// 为了方便输出
		int[] B = new int[n + 1];
		for (int i = 1; i <= n; i++) {
			int j = scanner.nextInt();
			if (!A[j]) {
				B[i] = 0;
			} else {
				B[i] = 1;
			}
		}
		for (int k = 1; k <= n; k++) {
			System.out.println(B[k]);
		}
	}
}

Violent solution: redundant code, high repetition rate, cumbersome and cumbersome

dp solution: There are only two results, he wins or I win, the relationship between right and wrong, use "||" to choose four methods, which means that as long as there is one method to win, then I will win in the end, and the method is the optimal value .

eg: 4 balls

1, 1, 1, 1 (I lost)

3, 1 (I won)

Traverse the calculation results and accumulate them


 

Guess you like

Origin blog.csdn.net/m0_54925305/article/details/123565978