The tenth-the decomposition of 04 numbers

topic

Decompose 2019 into the sum of 3 different positive integers, and require that each positive integer does not contain the numbers 2 and 4. How many different decomposition methods are there?
Note that the order of swapping the 3 integers is considered The same method, such as 1000+1001+18 and 1001+1000+18 are considered the same.

The original wrong approach

Thinking about it now, I'm so stupid (to put it nicely: simple), what people say is that positive integers do not contain 2 and 4, not 2 or 4! ! !
Moreover, a positive integer! What is a positive integer? ? ? Is a number greater than greater than greater than 0! ! ! !
I gave a wry smile.

public static void main(String[] args) {
    
    
		HashSet<List<Integer>> set = new HashSet<>();
		for (int i = 0; i <= 2019; i++) {
    
    
			if (i != 2 && i != 4) {
    
    
				for (int j = 0; j <= 2019; j++) {
    
    
					if (j != i && j != 4 && j != 2) {
    
    
						for (int k = 0; k <= 2019; k++) {
    
    
							if (k != i && k != j && k != 2 && k != 4) {
    
    
								if (i + j + k == 2019) {
    
    
									List<Integer> list = new ArrayList<>();
									list.add(i);
									list.add(j);
									list.add(k);
									Collections.sort(list);
									set.add(list);
								} else {
    
    
									break;
								}
							}
						}
					}
				}
			}
		}
		System.out.println(set.size());
	}

Parsing

Find the key points:
1. The total is 2019;
2. Three different numbers
3. Three different positive integers
4. Three different positive integers cannot contain 2 and 4
5. If there are duplicates The plan needs to be de-duplicated.

Correct way

public static void main(String[] args) {
    
    
		//设一个set用于去重
		HashSet<List<Integer>> set = new HashSet<>();
		//因为三个数都必须是正整数,所以i,j,k>=1
		for (int i = 1; i <= 2019; i++) {
    
    
			//判断一个数里面是否含有某个数字时,可以使用String.valueOf()
			String temp1 = String.valueOf(i);
			//判断
			if (!temp1.contains("2") && !temp1.contains("4")) {
    
    
				for (int j = 1; j <= 2019; j++) {
    
    
					String temp2 = String.valueOf(j);
					//i,j,k是三个不同的数
					if (i != j && !temp2.contains("2") && !temp2.contains("4")) {
    
    
						int k = 2019 - i - j;
						String temp3 = String.valueOf(k);
						if (k > 0 && k != i && k != j && !temp3.contains("2") && !temp3.contains("4")) {
    
    
							List<Integer> list = new ArrayList<>();
							list.add(i);
							list.add(j);
							list.add(k);
							Collections.sort(list);
							set.add(list);
						}
					}
				}
			}
		}
		System.out.println(set.size());
	}

Note here: in
order to reduce time complexity and space complexity, and improve efficiency: The value of k is calculated according to 2019-i-j. Put
i, j, and k into the list, sort them in order, and then add the set

grasp:

static String valueOf(boolean b) ——The string representation of the boolean parameter returned.
static String valueOf(char c)-The string representation of the returned char parameter.
static String valueOf(char[] data) ——The string representation of the returned char array parameter.
static String valueOf(char[] data, int offset, int count) —— Returns the string representation of the char array parameter of a specific subarray.
static String valueOf(double d) ——The string representation of the double parameter returned.
static String valueOf(float f)-The string representation of the returned float parameter.
static String valueOf(int i) —— The string representation of the returned int parameter.
static String valueOf(long l) ——The string representation of the returned long parameter.
static String valueOf(Object obj) ——The string representation of the returned Object parameter.

Reflection

Did not understand the question! Did not understand the question! Did not understand the question! ! ! !
In other words, I am not very proficient, and it is easy to take the sword off the front and then fall into a lost way.
Strengthen training! ! !
Think carefully: how to find a simpler or more suitable solution.

Guess you like

Origin blog.csdn.net/weixin_44998686/article/details/109121954
04