[Daily Blue Bridge] 19, 2014 Provincial Tournament Java Group Real Question "Martial Arts Cheats"

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: Wugong Cheats

Xiao Ming went to X cave to explore and found a damaged martial arts cheat book (more than 2,000 pages, of course it is a forgery).

He noticed that the 10th and 11th pages of the book are on the same sheet, but the 11th and 12th pages are not on the same sheet.

Xiao Ming only wants to practice the martial arts from page 81 to page 92 of the book, but doesn't want to bring the whole book with him. May I ask at least how many sheets of paper should he tear off and take away?

This is an integer, please submit the number through the browser, do not fill in any extra content

Problem-solving ideas:

This question is a fill-in-the-blank question, the key is to understand the question stem,

From the question we can get, if we divide each whole page into the left half page and the right half page,

Then it can be inferred from the fact that pages 10 and 11 are on the same sheet of paper, the left half of the book is an even-numbered page, and the right half of the book is an odd-numbered page.

Then we only need to determine how many adjacent even-odd numbers are within this range starting from page 81, and also consider whether the first page (page 81) and the last page (page 92) occupies a separate page.

The final answer is 7 pages.

Answer source code:

package 一四年省赛真题;

public class Year2014_Bt1 {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);	
		int x = scanner.nextInt();		//需要的第一页
		int y = scanner.nextInt();		//需要的最后一页
		int ans = 0;
		//判断起始页是不是奇数页,是的话页数加1
		if (x%2!=0) {
			ans++;
		}
		//判断终止页是不是偶数页,是的话页数加1
		if (y%2==0) {
			ans++;
		}
		
		for (int i = x; i <=y ; i+=1) {
			//如果该页是偶数页,且下一页是在需要的范围内,则页数加1
			if (i%2==0&&(i+1<=y)) {
				ans++;
			}
		}
		System.out.println(ans);

	}

}

 

 

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