Blue Bridge Cup 20200413

1. Decomposition of numbers

Decompose 2019 into the sum of three 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 exchanging 3 integers is regarded as the same method, for example, 1000 + 1001 + 18 and 1001 + 1000 + 18 are regarded as the same method.

First of all, we analyze the types of the three numbers that make up 2019?
1. There are six kinds of ABC arrangement (ABC, ACB, BAC, BCA,
CAB, CBA)
2. There are three kinds of AAB arrangement (AAB, ABA, BAA), and 3.AAA kind of arrangement.
The problem requires that 2019 be decomposed into the sum of three different positive integers, which means that only the combination of ABC classes is retained, j = i + 1,

public class Zichuan {	
	public static void main(String[] args) {
		int n = 2019;
		int num = 0;
		for (int i = 1; i < n; i++) {
			if ((i + "").indexOf("2") != -1 || (i + "").indexOf("4") != -1)
				continue;
			for (int j = i + 1; j < n; j++) {
				if ((j + "").indexOf("2") != -1 || (j + "").indexOf("4") != -1)
					continue;
				int k = n - i - j;
				if (i == k || j == k || i == j)
					continue;
				if (k > 0 && (k + "").indexOf("2") == -1 && (k + "").indexOf("4") == -1)
					num++;
			}
		}
		System.out.println(num / 3);
	}

}



2. Sum of special numbers

Xiao Ming is very interested in the numbers that contain 2, 0, 1, 9 (excluding the leading 0). In 1 to 40, such numbers include 1, 2, 9, 10 to 32, 39, and 40,
a total of 28 , Their sum is 574.
What is the sum of all such numbers from 1 to n?

public class Zichuan {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int result=0;
		for(int i=1;i<=n;i++) {
			String s=Integer.toString(i);
			if(s.contains("1")||s.contains("2")||s.contains("9")||s.contains("0")) {
				
				result=i+result;
			}
		}
		System.out.println(result);
	}
}


Published 44 original articles · Likes2 · Visits 540

Guess you like

Origin blog.csdn.net/qq_43699776/article/details/105498907