[Daily Blue Bridge] 26. One-five-year provincial Java group real question "Three Sheep Offerings"

Hello, I am the little gray ape, a programmer who can write bugs!

Welcome everyone to pay attention to my column " Daily Blue Bridge ". The main function of this column is to share with you the real questions of the Blue Bridge Cup provincial competitions and finals in recent years, analyze the algorithm ideas, data structures and other content that exist in it, and help you learn To more knowledge and technology!

Title: Sanyang Xianrui

Observe the following addition formula:

   Auspicious

+ Three sheep offer

------------------------

Three sheep are angry

(If there is an alignment problem, please refer to [Figure 1.jpg])

Among them, the same Chinese characters represent the same numbers, and different Chinese characters represent different numbers.

Please fill in the 4 digits represented by "Sanyang Xianrui" (the only answer), please do not fill in any extra content

Problem-solving ideas:

This question is relatively abstract, so we can change the question to express it, and replace the Chinese characters in the question with letters. We can infer the numbers represented by the letters in this formula according to the idea of ​​addition, as follows As shown in the figure:

For example: two four-digit numbers get a five-digit number, then the first digit of the five-digit number must be 1, that is to say e=1, since e=1 and a+e should be able to carry. , That is, a+e>=10, when e=1, obviously only a=9 meets the condition; then we get f=0, when f=0, b+f=c, and b must not be equal to c , Which means that b gets a carry, that is, c=b+1. Since it is a carry, then c+g will carry only when it is greater than 10. The reason why it cannot be equal to 10 is because f=0 causes b to fail Is equal to 0, so we get c+g>10,

So far, what we have inferred is: e=1, a=9, f=0, c=b+1, c+g>10, and the number represented by each letter cannot be the same.

In this way, we can use enumeration to enumerate the numbers that the letters of uncertain value may represent, and then determine whether the two numbers are added together to get the third number.

Answer source code:

public class Year2015_Bt3 {

	public static void main(String[] args) {
//		由于e=1,a=9,f=0已经确定,所以之后的字母应该避开这几个数字
		for (int b = 2; b < 9; b++) {
			int c = b+1;
			for (int d = 2; d < 9; d++) {
				//如果d等于a、b、c的任意一个数字,则进行下一次循环
				if (d==b||d==c) {
					continue;
				}
				for (int g = 2; g < 9; g++) {
//					判断g是否等于之前的任意一个数字
					if (g==b||g==c||g==d) {
						continue;
					}
					if (c+g>10) {
						int add1 = 9000+b*100+c*10+d;	//求出第一个加数
						int add2 = 1000+g*10+b;			//求出第二个加数
						for (int i = 2; i < 9; i++) {
//							判断i是否等于之前的任意一个数字
							if (i==b||i==c||i==d||i==g) {
								continue;
							}
							int addAns = 10000+c*100+b*10+i;	//求出两数的和
//							判断两数相加得到的结果是否等于和
							if (add1+add2==addAns) {
								System.out.println("10" + g + b);	
							}	
						}
						
					}
					
				}
				
			}	
		}
	}

}

 

Sample output:

There are deficiencies or improvements, and I hope that my friends will leave a message and learn together!

Interested friends can follow the column!

Little Gray Ape will accompany you to make progress together!

Guess you like

Origin blog.csdn.net/weixin_44985880/article/details/114490619
Recommended