[Daily Blue Bridge] 29. One-five-year provincial competition Java group real question "addition to multiplication"

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: Addition to Multiplication

We all know that 1+2+3+...+49=1225

Now you are required to turn the two non-adjacent plus signs into multiplication signs so that the result is 2015

For example: 1+2+3+...+9+10*11+12+...+27*28+29+...+49=2015

Is the answer that meets the requirements

Please look for another possible answer and submit the number before the multiplication sign in the front position (for example, 10 is required)

Note: What you need to submit is an integer, do not fill in any extra content

Problem-solving ideas:

For this question, we can first enumerate the positions of the first and second plus signs, and then judge the formula based on it. If you study the formula carefully, you can actually find:

Answer source code:

public class Year2015_Bt6 {

	public static void main(String[] args) {
		
		for (int i = 1; i <= 46; i++) {
			for (int j = i+2; j <= 48; j++) {
				//因为当加号变成乘号之后,除了i、i+1和j、j+1位变化,其他位均未变化,
				//所以只需要验证i*(i+1)-(i+i+1)+j*(j+1)-(j+j+1)的差值是否等于2015-1225即可
				if (i*(i+1)-(i+i+1)+j*(j+1)-(j+j+1)==2015-1225) {
					System.out.println(i + " " + j);			
				}
			}
			
		}
	}

}

 

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/114747907